automapper - 如果属性类型与属性名称不同,则忽略映射 - C#

问题描述:

如果属性类型与属性名称不同,如何忽略映射? 默认情况下,它是抛出错误。automapper - 如果属性类型与属性名称不同,则忽略映射 - C#

Mapper.CreateMap<EntityAttribute, LeadManagementService.LeadEntityAttribute>(); 

Model = Mapper.Map<EntityAttribute, LeadManagementService.LeadEntityAttribute>(EntityAttribute); 

我知道一种方法来指定要忽略的属性名称,但这不是我想要的。

.ForMember(d=>d.Field, m=>m.Ignore()); 

因为将来我可能会添加新的属性。所以我需要忽略具有不同数据类型的所有属性的映射。

+0

你试过.ForAllMembers(选择=>选择.Condition(IsValidType)));请参阅我的答案,例如源代码。 – Vinod

您应该使用忽略应该被忽略的属性:

Mapper.CreateMap<EntityAttribute, LeadManagementService.LeadEntityAttribute>() 
    ForMember(d=>d.FieldToIgnore, m=>m.Ignore()); 
+1

我可能会在未来添加新的属性。所以我需要忽略具有不同数据类型的所有属性的映射 – JerryGoyal

+0

您不能。你所能做的就是在配置上写自动测试,如果添加了问题字段,它会提醒你 –

一种方式来处理所有类型的属性是使用.ForAllMembers(选择=> opt.Condition(IsValidType)))。我已经使用了AutoMapper使用的新语法,但即使使用旧语法,它也应该可以工作。使用ForAllMaps()全球

Mapper.Initialize(cfg => 
{ 
    cfg.CreateMap<EntityAttribute, LeadEntityAttribute>().ForAllMembers(memberConf => 
    { 
     memberConf.Condition((ResolutionContext cond) => cond.DestinationType == cond.SourceType); 
    }); 
} 

您也可以应用它:

using System; 
using AutoMapper; 

namespace TestAutoMapper 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var mapperConfiguration = new MapperConfiguration(cfg => cfg.CreateMap<Car, CarDto>() 
       .ForAllMembers(opt => opt.Condition(IsValidType))); //and how to conditionally ignore properties 
      var car = new Car 
      { 
       VehicleType = new AutoType 
       { 
        Code = "001DXT", 
        Name = "001 DTX" 
       }, 
       EngineName = "RoadWarrior" 
      }; 

      IMapper mapper = mapperConfiguration.CreateMapper(); 
      var carDto = mapper.Map<Car, CarDto>(car); 
      Console.WriteLine(carDto.EngineName); 

      Console.ReadKey(); 
     } 

     private static bool IsValidType(ResolutionContext arg) 
     { 
      var isSameType = arg.SourceType== arg.DestinationType; //is source and destination type is same? 
      return isSameType; 
     } 
    } 

    public class Car 
    { 
     public AutoType VehicleType { get; set; } //same property name with different type 
     public string EngineName { get; set; } 
    } 

    public class CarDto 
    { 
     public string VehicleType { get; set; } //same property name with different type 
     public string EngineName { get; set; } 
    } 

    public class AutoType 
    { 
     public string Name { get; set; } 
     public string Code { get; set; } 
    } 
} 
+0

“ForAllMembers”不能应用于void类型的操作数。这个错误即将到来 – JerryGoyal

+0

你可以发布更多细节或更新你的问题你试过了吗? – Vinod

您可以使用ForAllMembers()设置相应的映射条件

Mapper.Initialize(cfg => 
{ 
    // register your maps here 
    cfg.CreateMap<A, B>(); 

    cfg.ForAllMaps((typeMap, mappingExpr) => 
    { 
     var ignoredPropMaps = typeMap.GetPropertyMaps(); 

     foreach (var map in ignoredPropMaps) 
     { 
      var sourcePropInfo = map.SourceMember as PropertyInfo; 
      if (sourcePropInfo == null) continue; 

      if (sourcePropInfo.PropertyType != map.DestinationPropertyType) 
       map.Ignore(); 
     } 
    }); 
}); 
+0

我尝试了第二个选项'ForAllMaps()',并在'map.Ignore()'的最后一行出现错误。未找到“忽略”方法。获取此错误=>'CS1061 \t'PropertyMap'不包含'Ignore'的定义,并且没有找到接受类型'PropertyMap'的第一个参数的扩展方法'Ignore'(您是否缺少using指令或程序集引用?)'想法? – Shiva

+0

@Shiva,试试'map.Ignored = true'。自答案写完以后,他们可能会更改API。 – haim770