AutoMapper和反射 [英] AutoMapper and reflection

查看:169
本文介绍了AutoMapper和反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的共享托管公司不允许反射. 如何使用AutoMapper?

My shared hosting company doesn't allow Reflection. How can I use AutoMapper?

我必须为每个属性指定一个.ForMember吗?

Do I have to specify for each property a .ForMember?

Mapper.CreateMap<Person, PersonData>()
            .ForMember(dest => dest.Name, o => o.MapFrom(src => src.Name))
            .ForMember(dest => dest.Address, o => o.MapFrom(src => src.Address));

谢谢

Filip

推荐答案

Automapper使用reflection.emit,确定要使用Automapper吗?

Automapper uses reflection.emit, are you sure you can use Automapper?

不知道有没有反射的用法,即使我在CodePlex上创建的 XmlDataMapper 也是使用反射的.没有反射或反射就很难设计一个.

Dont know of any that uses without reflection, even the one I had created XmlDataMapper on CodePlex uses reflection. It would difficult to design one without reflection or reflection.emit

最简单,最基本的方法是,您可以使用这两种技术中的任何一种.

The simplest and basic way to do this would be this, you can use any of the two or both techniques.

public class ConversionHelper
{
   public static ClassB Convert(ClassA item)
   {
      return new ClassB() { Id = item.Id, Name = item.Name };
   }

   public static List<ClassB> Convert(List<ClassA> list)
   {
      return list.Select(o => new ClassB() { Id = o.Id, Name = o.Name }).ToList();
   }
}


public class ClassA
{
   public int Id { get; set; }
   public string Name { get; set; }
}
public class ClassB
{
   public int Id { get; set; }
   public string Name { get; set; }
}

从给出的示例中,您始终试图一一映射属性,这是在同一行上,但是代码较少.

From the sample you have given where you are anyways trying to map property one by one, this is on the same lines, but with lesser code.

这篇关于AutoMapper和反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆