【发布时间】:2019-02-19 17:05:26
【问题描述】:
假设我有两种类型:
class Type1
{
public int Prop1 { get; set; }
public string Prop2 { get; set; }
public string Prop3 { get; set; }
}
class Type2
{
public int Prop1 { get; set; }
public string Prop2 { get; set; }
public TypeToIgnore Prop3 { get; set; }
}
我想在这两种类型之间进行映射,但忽略所有具有TypeToIgnore 的属性。这是因为我正在使用反射遍历所有这些并在它们上进行一些自定义映射。
在派生自 Profile 的类中,我可以为每个我不想映射的成员添加一个 Ignore,如下所示:
CreateMap<Type2, Type1>().ForMember(x => x.Prop3, y => y.Ignore());
或者我可以在要忽略的属性上使用IgnoreMapAttribute,但考虑到在生产代码中,我有很多,有没有更简单的方法来忽略某些特定类型?
【问题讨论】:
标签: c# automapper