C# AutoMapper IncludeBase()不使用泛型类型的参数?
可能我遗漏了一些明显的东西,但如果与参数的C# AutoMapper IncludeBase()不使用泛型类型的参数?,c#,.net,automapper,C#,.net,Automapper,可能我遗漏了一些明显的东西,但如果与参数的typeof()版本一起使用,则它似乎不起作用 这是可行的(LedgerEntryPersistent.Timestamp映射到LedgerEntryModel.Time): 我需要typeof()版本才能使用开放泛型(用于以外的其他类型) 一个可运行的示例: 我遗漏了什么?问题不在IncludeBase中,而是在MapFrom函数中 如果你愿意 cfg.CreateMap<SourceBase<String>, Destination
typeof()
版本一起使用,则它似乎不起作用
这是可行的(LedgerEntryPersistent.Timestamp
映射到LedgerEntryModel.Time
):
我需要typeof()
版本才能使用开放泛型(用于
以外的其他类型)
一个可运行的示例:
我遗漏了什么?问题不在
IncludeBase
中,而是在MapFrom
函数中
如果你愿意
cfg.CreateMap<SourceBase<String>, DestinationBase<String>>()
.ForMember("Time", mo => mo.MapFrom("Timestamp"));
cfg.CreateMap<Source, Destination>()
.IncludeBase<SourceBase<String>, DestinationBase<String>>();
它不起作用了
尝试使用泛型函数进行基映射,使用string
函数进行子映射
cfg.CreateMap<SourceBase<String>, DestinationBase<String>>()
.ForMember(x=>x.Time, mo => mo.MapFrom(s=>s.Timestamp));
cfg.CreateMap(typeof(Source), typeof(Destination))
.ForMember("Y", mo=>mo.MapFrom("X"))
.IncludeBase(typeof(SourceBase<String>), typeof(DestinationBase<String>));
用法
cfg.CreateMap(typeof(SourceBase<String>), typeof(DestinationBase<String>))
.ForMember("Time",
mo => mo.MapFrom(MapValue("Timestamp", typeof(SourceBase<string>))));
cfg.CreateMap(typeof(SourceBase),typeof(DestinationBase))
.ForMember(“时间”,
mo=>mo.MapFrom(MapValue(“Timestamp”,typeof(SourceBase)));
这是一个将在AutoMapper 6.2.0中修复的错误:
在新版本发布之前,请参阅Aspiri的答案以了解解决方法。不能使用泛型CreateMap,因为SourceBase需要处理任何类型参数,而不仅仅是字符串。您没有要映射的子类吗?是的,每个子类都可以使用不同的泛型基类
Child1:SourceBase
,Child2:SourceBase
,Child3:SourceBase
等等。我需要可以忽略type参数并映射Time[stamp]属性的映射,该属性不会更改。这意味着您必须执行cfg.CreateMap(typeof(IntSource)、typeof(IntDestination))
和cfg.CreateMap(typeof(StringSource)、typeof(StringDestination))
等操作,对吗?或者有一些动态映射构建工具?我希望避免重复相同的映射Timestamp
->Time
在所有情况下都是相同的属性,如果我有15个版本的SourceBase
,我不想写15次.ForMember(x=>x.Time,mo=>mo.MapFrom(s=>s.Timestamp))代码>
.ForMember(x=>x.Time, mo => mo.MapFrom("Timestamp"));
cfg.CreateMap<SourceBase<String>, DestinationBase<String>>()
.ForMember(x=>x.Time, mo => mo.MapFrom(s=>s.Timestamp));
cfg.CreateMap(typeof(Source), typeof(Destination))
.ForMember("Y", mo=>mo.MapFrom("X"))
.IncludeBase(typeof(SourceBase<String>), typeof(DestinationBase<String>));
IMappingExpression<SourceBase<T>, DestinationBase<T>>
GetBaseMap<T>(IMapperConfigurationExpression cfg)
{
return cfg.CreateMap<SourceBase<T>, DestinationBase<T>>()
.ForMember(x => x.Time, mo => mo.MapFrom(s => s.Timestamp));
}
cfg.CreateMap(typeof(StrSource), typeof(StrDestination)); // + specific mappings
GetBaseMap<string>(cfg)
.Include(typeof(StrSource), typeof(StrDestination));
public Expression<Func<object, object>> MapValue(string name, Type sourceType)
{
var parameter = Expression.Parameter(typeof(object));
var objectParameter = Expression.Convert(parameter, sourceType);
var property = Expression.PropertyOrField(objectParameter, name);
var propertyConverted = Expression.Convert(property, typeof(object));
return Expression.Lambda<Func<object, object>>(propertyConverted, parameter);
}
cfg.CreateMap(typeof(SourceBase<String>), typeof(DestinationBase<String>))
.ForMember("Time",
mo => mo.MapFrom(MapValue("Timestamp", typeof(SourceBase<string>))));