簡體   English   中英

如何告訴Automapper檢查所有源屬性是否都具有目標屬性

[英]How to tell Automapper to check if all source properties have destination properties

我們有兩個類:

public class Foo
{
    public int A { get; set; }
    public int B { get; set; }
    public int C { get; set; }
}

public class Bar
{
    public int A { get; set; }
    public int B { get; set; }
} 

和映射配置

 Mapper.CreateMap<Foo, Bar>;

Automapper是否有可能自動檢查所有源屬性是否具有對應的目標屬性,在我的示例中,拋出一個異常,通知我們有關Foo.C屬性未映射到任何內容的異常。 Mapper.AssertConfigurationIsValid()僅以另一種方式進行檢查-所有目標屬性都具有源屬性,因此對我而言無濟於事。

也許您可以使用hack並從另一個方向測試映射。 就像是:

Mapper.CreateMap<Bar, Foo>; // Swap the direction of the mapping
Mapper.AssertConfigurationIsValid()

我知道這不是理想的選擇,但可以快速解決。

暫無
暫無

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

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