繁体   English   中英

在没有泛型的情况下配置AutoMapper

[英]Configuring AutoMapper without Generics

我想在不使用泛型的情况下配置AutoMapper,因为我想在运行时进行配置。

我想配置SubstiteNulls方法,并能够执行以下操作:

Mapper.CreateMap<Source, Dest>()
    .ForMember(dest => dest.Value, opt => opt.NullSubstitute("Other Value"));

但是我不知道该怎么做。 您可以将它们的Type对象传递给CreateMap工厂方法,但是当您使用ForMember方法时, opt对象不包含NullSubstitute方法,我想这是由于我在这里使用的泛型缺乏。

关于如何实现此目标的任何想法?

更新资料

这些是我得到的选择:

在此处输入图片说明

当前,在使用非通用版本的CreateMap时,将在IMappingExpression接口上使用NullSubstitute配置。

没有限制,可以阻止IMappingExpressionIMappingExpression上使用此方法,因此当前不支持此方法。

您有三种选择:

  • 在Github上创建问题,并等待其实施
  • 分叉项目并自己实现方法。 您可以使用通用版本作为示例非常容易。
  • 或者,如果您想要快速但非常肮脏的解决方案。 通过反射,您可以从配置中获取底层的PropertyMap并对其调用SetNullSubstitute方法:

     Mapper.CreateMap(typeof(Source), typeof(Dest)) .ForMember("Value", opt => { FieldInfo fieldInfo = opt.GetType().GetField("_propertyMap", BindingFlags.Instance | BindingFlags.NonPublic); var propertyMap = (PropertyMap) fieldInfo.GetValue(opt); propertyMap.SetNullSubstitute("Null Value"); }); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM