我知道如何在映射定义时忽略属性。但是,我正在尝试向所有映射添加一个通用逻辑,以忽略基于某些条件的属性。为此,我使用的代码如下所示。
public static void ProcessMappings(this IMappingEngine engine)
{
var typeMaps = engine.ConfigurationProvider.GetAllTypeMaps();
foreach (var typeMap in typeMaps)
{
if (typeof(MyClass).IsAssignableFrom(typeMap.DestinationType))
{
// here let's say I want to ignore property "Property"
}
}
}因此,我的问题是,给定一个TypeMap实例,我如何设置要忽略的属性?
发布于 2016-09-16 19:21:34
var propInfo = typeMap.DestinationType.GetProperty("PropertyToIgnore");
if (propInfo != null)
{
typeMap.FindOrCreatePropertyMapFor(new AutoMapper.Impl.PropertyAccessor(propInfo)).Ignore();
}Ignore方法用于忽略属性/成员
https://stackoverflow.com/questions/39529365
复制相似问题