Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当源属性未映射时强制引发异常_C#_Automapper - Fatal编程技术网

C# 当源属性未映射时强制引发异常

C# 当源属性未映射时强制引发异常,c#,automapper,C#,Automapper,在AutoMapper 2.2.1中,是否有任何方法可以配置映射,以便在未显式忽略属性时引发异常?例如,我有以下类和配置: 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 i

在AutoMapper 2.2.1中,是否有任何方法可以配置映射,以便在未显式忽略属性时引发异常?例如,我有以下类和配置:

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>();
Mapper.AssertConfigurationIsValid();
然后我将不会收到任何映射异常。我希望发生的是抛出
AutoMapperConfigurationException
,因为没有显式忽略
Source.Z

我希望我必须显式忽略Z属性,以便
assertconfigurationsvalid
无异常运行:

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

目前,AutoMapper不会引发异常。如果我没有明确指定
Ignore
,我希望它抛出一个异常。如何执行此操作?

以下是一个方法,该方法断言所有源类型属性都已映射:

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));
        }
    }
}
public静态void AssertAllSourcePropertiesMapped()
{
foreach(Mapper.GetAllTypeMaps()中的变量映射)
{
//这里是hack,因为源成员映射没有公开
类型t=类型(类型映射);
var configs=t.GetField(“_sourceMemberConfigs”,BindingFlags.Instance | BindingFlags.NonPublic);
var mappedSourceProperties=((IEnumerable)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)
{
如果(!mappedProperties.Contains(propertyInfo))
抛出新异常(String.Format(“未映射类型为“{1}”的属性“{0}”),
propertyInfo,map.SourceType));
}
}
}
它检查所有配置的映射,并验证每个源类型属性是否定义了映射(映射或忽略)

用法:

Mapper.CreateMap<Source, Destination>();
// ...
AssertAllSourcePropertiesMapped();
Mapper.CreateMap();
// ...
AssertAllSourcePropertiesMapped();
这是个例外

未映射类型为“YourNamespace.Source”的属性“Int32 Z”

如果忽略该属性,则一切正常:

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

TypeMap@MikeBantegui完全同意您的观点,但是源成员映射没有在
TypeMap上公开,这让人感到遗憾。在答案的第一次编辑中,我只是使用了
GetPropertyMaps()
,但令我惊讶的是,这里既没有源映射,也没有源映射作为TypeMap的公共属性公开