2016-06-28 278 views
6

如果屬性類型與屬性名稱不同,如何忽略映射? 默認情況下,它是拋出錯誤。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

回答

4

您應該使用忽略應該被忽略的屬性:

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

我可能會在未來添加新的屬性。所以我需要忽略具有不同數據類型的所有屬性的映射 – JerryGoyal

+0

您不能。你所能做的就是在配置上寫自動測試,如果添加了問題字段,它會提醒你 –

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

3

您可以使用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

相關問題