自动映射器从内部属性映射到目标类 [英] Automapper map from inner property to destination class

查看:89
本文介绍了自动映射器从内部属性映射到目标类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎无法弄清楚这一点.

Cant' seem to figure this one out.

public class DestinationClass
{
    public int InnerPropertyId { get; set; }
    public string StrignValue { get; set; }
}

public class SourceClass
{
    public InnerValue Inner { get; set; }
}

public class InnerValue
{
    public int InnerPropertyId { get; set; }
    public string StrignValue {get;set;}
}

我需要直接从SourceClass.InnerValue映射到DestinationClass.我怎么做?

I need to map from SourceClass.InnerValue directly to DestinationClass. How do I do that?

谢谢.

推荐答案

和往常一样,在我点击发布问题"按钮后:

As usual, right after I hit post question button:

Mapper.Reset();
// from, to
Mapper.CreateMap<InnerValue, DestinationClass>();
Mapper.CreateMap<SourceClass, DestinationClass>()
    .ConvertUsing(s => Mapper.Map<InnerValue, DestinationClass>(s.Inner));

Mapper.AssertConfigurationIsValid();

var source = new SourceClass() { Inner = new InnerValue() { InnerPropertyId = 123, StringValue = "somethinges" } };

var dest = Mapper.Map<SourceClass, DestinationClass>(source);

这篇关于自动映射器从内部属性映射到目标类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆