简体   繁体   English

如何告诉Automapper检查所有源属性是否都具有目标属性

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

We have two classes: 我们有两个类:

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; }
} 

and mapping configuration 和映射配置

 Mapper.CreateMap<Foo, Bar>;

Is there some possibility for Automapper to check automatically that all source properties have corresponding destination properties, in my example throwing an exception which notifies us about Foo.C property not mapped to anything. Automapper是否有可能自动检查所有源属性是否具有对应的目标属性,在我的示例中,抛出一个异常,通知我们有关Foo.C属性未映射到任何内容的异常。 Mapper.AssertConfigurationIsValid() checks only the other way round - that all destination properties have source properties so it doesn't help in my case. Mapper.AssertConfigurationIsValid()仅以另一种方式进行检查-所有目标属性都具有源属性,因此对我而言无济于事。

Maybe you can use a hack and test the mapping in the other direction. 也许您可以使用hack并从另一个方向测试映射。 Something like: 就像是:

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

I know it isn't ideal but could be a quick solution. 我知道这不是理想的选择,但可以快速解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Automapper如果source为null,则设置目标对象属性 - Automapper If source is null, set destination object properties AutoMapper:如果指定类型的源对象为null,则将目标对象的所有属性设置为默认值 - AutoMapper: Set all properties of destination object to default if source object is null for specified types 源中具有只读属性的 AutoMapper 行为未映射到目标 - AutoMapper behaviour with readonly properties in source not being mapped to destination Automapper - 它可以仅映射源对象和目标对象中的现有属性吗? - Automapper - can it map over only existing properties in source and destination objects? AutoMapper覆盖源属性 - AutoMapper overrides source properties Automapper-映射除目标中不存在的属性以外的所有属性 - Automapper - map all properties except the one that does not exist in destination 如果它们是默认值,如何告诉自动映射器从源类型跳过映射属性? - How to tell automapper to skip mapping properties from source type if they are default values? Automapper是否可以将复杂的源图映射到平面目标,而在目标属性中没有前缀并且没有自定义映射? - Can Automapper map a complex source graph to a flat destination without prefixes in the destination properties and without custom mappings? 如何使用 Automapper 在目标 object 中保留集合的未映射属性? - How to preserve the not mapped properties of a collection in a destination object with Automapper? AutoMapper:通过两个属性生成目标 - AutoMapper: Build destination from two properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM