The differences between a normal map with dynamic

2019-02-22 02:17发布

问题:

What are the differences between the codes below;

    List<Ogrenci> ogrenci = AutoMapper.Mapper.DynamicMap<IDataReader,   List<Ogrenci>>((dt.CreateDataReader()));

    var ogr = AutoMapper.Mapper.Map<IDataReader, IList<Ogrenci>>(dt.CreateDataReader());

When i try to use the code below;

 AutoMapper.Mapper.CreateMap<IDataReader, Ogrenci>().ForMember(dest => dest.Numarasi, opt => opt.MapFrom(src => Convert.ToInt32(src["Numara"])))
            .ForMember(dest => dest.Adi, opt => opt.MapFrom(src => Convert.ToString(src["Ad"])))
            .ForMember(dest => dest.Soyadi, opt => opt.MapFrom(src => Convert.ToString(src["Soyad"])));

        List<Ogrenci> ogrenci = AutoMapper.Mapper.DynamicMap<IDataReader, List<Ogrenci>>((dt.CreateDataReader()));
        var ogr = AutoMapper.Mapper.Map<IDataReader, IList<Ogrenci>>(dt.CreateDataReader());

I can not use use the code below;

  List<Ogrenci> ogrenci = AutoMapper.Mapper.DynamicMap<IDataReader, List<Ogrenci>>((dt.CreateDataReader()));

but i can use the code below ;

   var ogr = AutoMapper.Mapper.Map<IDataReader, IList<Ogrenci>>(dt.CreateDataReader());

What are your suggestions?

Thank you.

回答1:

DynamicMap in AutoMapper is used when you don't know the source type at compile time. The reason you can't use

List<Ogrenci> ogrenci = AutoMapper.Mapper.DynamicMap<IDataReader, List<Ogrenci>>((dt.CreateDataReader()));

is because DynamicMap doesn't need you to specify the source type, only the destination type.

AutoMapper features: Dynamic Mapping



标签: c# AutoMapper