簡體   English   中英

Automapper - >將對象的命名屬性映射到數組屬性

[英]Automapper -> map named properties of object to an array property

所以我有一個有數組的對象,我需要將另一個對象的屬性映射到這個數組。 在Automapper 5中,我曾經使用“ResolveUsing”執行此操作,但在更新了automapper之后,這不起作用。 我之前做的是:

.ForMember(dest => dest.Array, opt => opt.ResolveUsing(o =>
                {
                    return new[] {
                        new Arra() { Key = "Key", Value = o.Value },
                        new Arra() { Key = "Key2", Value = o.Value2 }
                    };
                }));

在升級指南中,他們提到不再使用ResolveUsing: http ://docs.automapper.org/en/stable/8.0-Upgrade-Guide.html閱讀完之后我實際上並不知道如何解決這個問題。

所有搜索都會引導我找到人們嘗試映射數組的問題 - >命名屬性,而我的另一種方式。

您是否嘗試使用MapFrom而不是Resolve?

.ForMember(dest => dest.Array, opt => opt.MapFrom(o => new[] {
                    new Arra() { Key = "Key", Value = o.Value },
                    new Arra() { Key = "Key2", Value = o.Value2 }
            }));

這應該適合你,我相信 -

.ForMember(dest => dest.Array, opt => opt.MapFrom(o => new[] {
                        new Arra() { Key = "Key", Value = o.Value },
                        new Arra() { Key = "Key2", Value = o.Value2 }
                    }));

暫無
暫無

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

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