automapper - ignore mapping if property type is different with same property name - C#

后端 未结 3 1289
抹茶落季
抹茶落季 2021-01-12 03:24

How can I ignore mapping if property type is different with same property name? By default it\'s throwing error.

Mapper.CreateMap

        
相关标签:
3条回答
  • 2021-01-12 04:03

    You should use ignore for properties that should be ignored:

    Mapper.CreateMap<EntityAttribute, LeadManagementService.LeadEntityAttribute>()
         ForMember(d=>d.FieldToIgnore, m=>m.Ignore());
    
    0 讨论(0)
  • 2021-01-12 04:16

    One way to process all properties for type is to use .ForAllMembers(opt => opt.Condition(IsValidType))). I have used new syntax for AutoMapper usage, but it should work even with old syntax.

    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 讨论(0)
  • 2021-01-12 04:21

    You can use ForAllMembers() to setup the appropriate mapping condition:

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

    You can also apply it globally using ForAllMaps():

    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 讨论(0)
提交回复
热议问题