简体   繁体   English

自动映射器映射通用类型

[英]Automapper mapping generic types

I have 2 generic types that I need to map 我有2种通用类型需要映射

public class A<T>
{
    public List<T> Results { get; set; }
    public int PropertyA { get; set; }
    public int PropertyB { get; set; }
}

and

public class B<T>
{
    public List<T> ResultContent { get; set; }
    public int PropertyC { get; set; }  
}

I've tried to map A to B using automapper as follows: 我尝试使用自动映射器将A映射到B,如下所示:

Mapper.CreateMap(typeof(A<>),typeof(B<>))
    .ForMember("ResultContent", f => f.MapFrom("Results"))
    .ForMember("PropertyC", f => f.MapFrom(?????))

It works fine for the ResultContent property. 对于ResultContent属性,它工作正常。 However, the issue is that B.PropertyC is a sum of properties A.PropertyA and A.PropertyB. 但是,问题在于B.PropertyC是属性A.PropertyA和A.PropertyB的总和。 Is it possible to compute mapping of properties for generic types? 是否可以计算泛型类型的属性映射?

For this you have to use a converter and ResolveUsing() which will define the operation to be performed. 为此,您必须使用converterResolveUsing()来定义要执行的操作。

See for instance this answer for more details. 例如,请参阅此答案以获取更多详细信息。

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

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