简体   繁体   中英

Automapper mapping generic types

I have 2 generic types that I need to map

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:

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

It works fine for the ResultContent property. However, the issue is that B.PropertyC is a sum of properties A.PropertyA and 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.

See for instance this answer for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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