![](/img/trans.png)
[英]Automapper If source is null, set destination object properties
[英]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.