让我们假设我有两种类型:
Let's suppose that I have two types:
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的属性.这是因为我正在使用反射来遍历所有这些对象,并在它们上进行一些自定义映射.
I want to map between these two types, but ignoring all the properties that have a TypeToIgnore. This is because I am iterating through all of them using reflection and make some custom mappings on them.
在从Profile派生的类中,我可以为不想映射的每个成员添加一个Ignore,如下所示:
Within a class which derives from Profile, I could add an Ignore for each member that I don't want to be mapped, like this:
CreateMap<Type2, Type1>().ForMember(x => x.Prop3, y => y.Ignore());或者我可以在要忽略的属性上使用IgnoreMapAttribute,但是考虑到在生产代码上我有很多,是否有一种更简单的方法可以完全忽略某些特定类型?
Or I could use the IgnoreMapAttribute on the properties to be ignored, but considering that on the production code, I have plenty of them, is there a much easier way to ignore some specific types at all?
推荐答案您可以在配置中使用ShouldMapProperty:
cfg.ShouldMapProperty = p => p.PropertyType != typeof(string);此官方文档位于此处.真正由您发出的原始功能请求.
Official docs on this here. Original feature request by yours truly.
更多推荐
使用Automapper时如何忽略特定类型的属性?
发布评论