有没有一种方法告诉AutoMapper忽略所有的属性,除了那些显式映射的?
我有可能从外部更改的外部DTO类,并且我希望避免显式地指定要忽略的每个属性,因为添加新属性将在尝试将它们映射到自己的对象时破坏功能(导致异常)。
有没有一种方法告诉AutoMapper忽略所有的属性,除了那些显式映射的?
我有可能从外部更改的外部DTO类,并且我希望避免显式地指定要忽略的每个属性,因为添加新属性将在尝试将它们映射到自己的对象时破坏功能(导致异常)。
当前回答
当前(版本9)忽略目标类型中不存在的属性的解决方案是创建一个翻转映射并反转它:
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<TheActualDestinationType, TheActualSourceType>().ReverseMap();
});
其他回答
在dotnet 5的WebApi中,使用Nuget包automapper . extensions .微软。DependencyInjection,我在一个映射器配置文件中这样做。我对AutoMapper真的生疏了,但它现在似乎对未映射的成员工作得很好。
在启动:
var mapperConfig = new MapperConfiguration(mc => mc.AddProfile(new AutoMapperProfile()));
services.AddSingleton(mapperConfig.CreateMapper());
在我的AutoMapperProfile中:
CreateMap<ProjectActivity, Activity>()
.ForMember(dest => dest.ActivityName, opt => opt.MapFrom(src => src.Name))
.ValidateMemberList(MemberList.None);
这是我编写的一个扩展方法,它忽略目标上所有不存在的属性。不确定它是否仍然有用,因为这个问题已经存在两年多了,但我遇到了同样的问题,必须添加大量手动忽略调用。
public static IMappingExpression<TSource, TDestination> IgnoreAllNonExisting<TSource, TDestination>
(this IMappingExpression<TSource, TDestination> expression)
{
var flags = BindingFlags.Public | BindingFlags.Instance;
var sourceType = typeof (TSource);
var destinationProperties = typeof (TDestination).GetProperties(flags);
foreach (var property in destinationProperties)
{
if (sourceType.GetProperty(property.Name, flags) == null)
{
expression.ForMember(property.Name, opt => opt.Ignore());
}
}
return expression;
}
用法:
Mapper.CreateMap<SourceType, DestinationType>()
.IgnoreAllNonExisting();
更新:显然,如果您有自定义映射,这不能正确工作,因为它会覆盖它们。我想如果先调用IgnoreAllNonExisting,然后再调用自定义映射,它仍然可以工作。
schdr有一个解决方案(作为这个问题的答案),它使用map . getalltypemaps()来找出哪些属性未映射并自动忽略它们。在我看来这是个更可靠的解决方案。
在3.3.1版本中,您可以使用IgnoreAllPropertiesWithAnInaccessibleSetter()或IgnoreAllSourcePropertiesWithAnInaccessibleSetter()方法。
版本12 这是一个非常糟糕的代码,但它解决了紧急工作。
CreateMap<RepairPutRequest, Repair>(MemberList.None)
.ForAbdusselamMember(x => x.ClosedLostTimeId, y => y.MapFrom(z => z.ClosedLostTimeId))
.ForAbdusselamMember(x => x.Explanation, y => y.MapFrom(z => z.Explanation)).IgnoreUnmapped()
;
public static class MappingExtensions
{
public static Dictionary<string, List<string>> list = new Dictionary<string, List<string>>();
public static IMappingExpression<TSource, TDestination> IgnoreUnmapped<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
var sourceType = typeof(TSource);
var destinationType = typeof(TDestination);
var key =$"{sourceType.FullName}__||__{destinationType.FullName}" ;
var t = list[key];
foreach (var item in destinationType.GetProperties())
{
if (!t.Contains(item.Name)) {
expression.ForMember(item.Name, x => x.Ignore());
}
}
return expression;
}
public static IMappingExpression<TSource, TDestination> ForAbdusselamMember<TSource, TDestination, TMember>(this IMappingExpression<TSource, TDestination> expression,
Expression<Func<TDestination, TMember>> destinationMember, Action<IMemberConfigurationExpression<TSource, TDestination, TMember>> memberOptions )
{
var sourceType = typeof(TSource);
var destinationType = typeof(TDestination);
var key = $"{sourceType.FullName}__||__{destinationType.FullName}";
if (!list.ContainsKey(key))
{
list[key]=new List<string>();
}
expression.ForMember(destinationMember, memberOptions);
var memberInfo = ReflectionHelper.FindProperty(destinationMember);
list[key].Add(memberInfo.Name);
return expression;
}
}
从AutoMapper 5.0开始,IMappingExpression上的. typemap属性消失了,这意味着4.2解决方案不再有效。我已经创建了一个解决方案,使用原始功能,但具有不同的语法:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Src, Dest>();
cfg.IgnoreUnmapped(); // Ignores unmapped properties on all maps
cfg.IgnoreUnmapped<Src, Dest>(); // Ignores unmapped properties on specific map
});
// or add inside a profile
public class MyProfile : Profile
{
this.IgnoreUnmapped();
CreateMap<MyType1, MyType2>();
}
实现:
public static class MapperExtensions
{
private static void IgnoreUnmappedProperties(TypeMap map, IMappingExpression expr)
{
foreach (string propName in map.GetUnmappedPropertyNames())
{
if (map.SourceType.GetProperty(propName) != null)
{
expr.ForSourceMember(propName, opt => opt.Ignore());
}
if (map.DestinationType.GetProperty(propName) != null)
{
expr.ForMember(propName, opt => opt.Ignore());
}
}
}
public static void IgnoreUnmapped(this IProfileExpression profile)
{
profile.ForAllMaps(IgnoreUnmappedProperties);
}
public static void IgnoreUnmapped(this IProfileExpression profile, Func<TypeMap, bool> filter)
{
profile.ForAllMaps((map, expr) =>
{
if (filter(map))
{
IgnoreUnmappedProperties(map, expr);
}
});
}
public static void IgnoreUnmapped(this IProfileExpression profile, Type src, Type dest)
{
profile.IgnoreUnmapped((TypeMap map) => map.SourceType == src && map.DestinationType == dest);
}
public static void IgnoreUnmapped<TSrc, TDest>(this IProfileExpression profile)
{
profile.IgnoreUnmapped(typeof(TSrc), typeof(TDest));
}
}