簡體   English   中英

Automapper 從外部類映射到內部類變量

[英]Automapper Map from outer class to inner class variable

我正在使用 Automapper 並且需要知道是否可以將源變量映射到嵌套成員變量?

這是我要映射的來源

public class source
{
    public string name;
}

這是目的地 - 我需要分配給 Nested.Name 成員的 name 變量

public class Destination
{
    public Nested info;
}

public class Nested
{
    public string name;
}

非常感謝任何幫助。 羅恩。

ForPath可以解決問題

CreateMap<source, Destination>()
        .ForPath(d => d.info.name, c => c.MapFrom(src => src.name));

這樣的事情應該可以解決問題,盡管您可能會遇到未初始化的dst.info

CreateMap<source, Destination>()
            //reegular mapping here
            .ForMember(dst => dst.foo, c => c.MapFrom(src => src.otherfoo))
            //AfterMap to bind your properties
            .AfterMap((src, dst) => { dst.info.name = src.name; });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM