本文介绍了未映射源属性时强制引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AutoMapper 2.2.1中,有什么方法可以配置映射,以便在不显式忽略属性时引发异常?例如,我具有以下类和配置:

In AutoMapper 2.2.1, is there any way I can configure my mappings so that when a property is not explicitly ignored, an exception is thrown? For example, I have the following classes and configuration:

public class Source
{
    public int X { get; set; }
    public int Y { get; set; }
    public int Z { get; set; }
}

public class Destination
{
    public int X { get; set; }
    public int Y { get; set; }
}


// Config
Mapper.CreateMap<Source, Destination>();

使用此配置收到的行为是设置了Destination.XDestination.Y属性.此外,如果我测试我的配置:

The behavior I receive with this configuration is that the Destination.X and Destination.Y properties are set. Furthermore, if I test my configuration:

Mapper.AssertConfigurationIsValid();

然后,我将不会收到任何映射异常.我想发生的是,因为未明确忽略Source.Z,所以抛出了AutoMapperConfigurationException.

Then I will receive no mapping exceptions. What I would like to happen is that an AutoMapperConfigurationException is thrown because Source.Z is not explicitly ignored.

我希望这样做,以便必须显式忽略Z属性,以便AssertConfiguartionIsValid正常运行:

I would like it so that I have to explicitly ignore the Z property in order for AssertConfiguartionIsValid to run without exceptions:

Mapper.CreateMap<Source, Destination>()
      .ForSourceMember(m => m.Z, e => e.Ignore());

当前,AutoMapper 不会引发异常.如果我未明确指定Ignore,我希望它引发异常.我该怎么办?

Currently, AutoMapper does not throw an exception. I would like it to throw an exception if I do not explicitly specify the Ignore. How can I do this?

推荐答案

以下是断言所有源类型属性都已映射的方法:

Here is method which asserts that all source type properties are mapped:

public static void AssertAllSourcePropertiesMapped()
{
    foreach (var map in Mapper.GetAllTypeMaps())
    {
        // Here is hack, because source member mappings are not exposed
        Type t = typeof(TypeMap);
        var configs = t.GetField("_sourceMemberConfigs", BindingFlags.Instance | BindingFlags.NonPublic);
        var mappedSourceProperties = ((IEnumerable<SourceMemberConfig>)configs.GetValue(map)).Select(m => m.SourceMember);

        var mappedProperties = map.GetPropertyMaps().Select(m => m.SourceMember)
                                  .Concat(mappedSourceProperties);

        var properties = map.SourceType.GetProperties(BindingFlags.Instance | BindingFlags.Public);

        foreach (var propertyInfo in properties)
        {
            if (!mappedProperties.Contains(propertyInfo))
                throw new Exception(String.Format("Property '{0}' of type '{1}' is not mapped",
                                                  propertyInfo, map.SourceType));
        }
    }
}

它检查所有已配置的映射,并验证每个源类型属性是否定义了映射(已映射或已忽略).

It checks all configured mappings and verifies that each source type property has mapping defined (either mapped, or ignored).

用法:

Mapper.CreateMap<Source, Destination>();
// ...
AssertAllSourcePropertiesMapped();

这会引发异常

如果您将忽略该属性,那一切都很好:

If you will ignore that property, all is fine:

Mapper.CreateMap<Source, Destination>()
      .ForSourceMember(s => s.Z, opt => opt.Ignore());
AssertAllSourcePropertiesMapped();

这篇关于未映射源属性时强制引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:22