3 Replies - 666 Views - Last Post: 27 March 2013 - 10:27 AM Rate Topic: -----

#1 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

passing dynamic types to a generic method?

Posted 27 March 2013 - 07:39 AM

    public class ReservationRepository: iRepository<IReservationDTO> 
    {
        public BaseReservationContext Context { get; set; } // new up context
        public IReservationMappings Map { get; set; } // new up map
 
        public void Add(IReservationDTO _resDTO)
        {

            BaseReservationEntity res = Map.ReservationMappingDTO<IReservationDTO, BaseReservationEntity>(_resDTO);
            Context.Reservations.Add(res);
            Context.SaveChanges();
         
        }



BaseReservationEntity res = Map.ReservationMappingDTO<IReservationDTO, BaseReservationEntity>(_resDTO);



I would like MapReservationMappingDTO generic method to have its generic properties filled with the types of both the _resDTO and Context instance.

something life this

Map.ReservationMappingDTO<context.type, _resdto.type>(_resdto)

any ideas

This post has been edited by ray1234: 27 March 2013 - 07:41 AM


Is This A Good Question/Topic? 0
  • +

Replies To: passing dynamic types to a generic method?

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: passing dynamic types to a generic method?

Posted 27 March 2013 - 07:54 AM

Generics are compile time constructs, not runtime constructs. You cannot use a System.Type, it must be an actual type identifier.
Was This Post Helpful? 0
  • +
  • -

#3 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: passing dynamic types to a generic method?

Posted 27 March 2013 - 08:13 AM

View PostCurtis Rutland, on 27 March 2013 - 02:54 PM, said:

Generics are compile time constructs, not runtime constructs. You cannot use a System.Type, it must be an actual type identifier.


ok fair enough. my issue is that i'm using for my reservationmapping method i'm using auto mapper

Mapper.CreateMap<ireservationdto,reservationentity>();



one is an interface the other an abstarct type both passed in generic parameter. I don't want to pass concreate types directly e.g.

Mapper.CreateMap<reservationdto, resentity> (); // don't want to do

which both inherit from ireservationdot and reservation entity. Doing this would potential break the open close principle in the future and creates futher depedency on other layers.

so is there a way around doing this?

thanks

This post has been edited by ray1234: 27 March 2013 - 08:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: passing dynamic types to a generic method?

Posted 27 March 2013 - 10:27 AM

Problem now solved
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1