automapper, mapping to an interface

落花浮王杯 提交于 2019-12-05 10:31:33

Something like this:

class Program
{
    static void Main(string[] args)
    {
        Mapper.Initialize(x => x.AddProfile<MappingProfile>());

        var a = new A()
        {
            MyI = new CA()
            {
                StringProp2 = "sp2"
            }
        };

        var b = Mapper.Map<A, B>(a);

        Console.WriteLine("a.MyI.StringProp1: " + a.MyI.StringProp1);
        Console.WriteLine("b.MyI.StringProp1: " + b.MyI.StringProp1);

    }
}

>= AutoMapper 2.0.0

public class MappingProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<CA, CB>();
        CreateMap<CA, I2>().As<CB>();                                                                       
        CreateMap<A, B>();
    }
}

AutoMapper 1.1.0.188 (.Net 3.5)

public class MappingProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<CA, CB>();

        CreateMap<CA, I2>()
            .ConstructUsing(Mapper.Map<CA, CB>)
            ;

        CreateMap<A, B>();
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!