c# - 自动映射器 3 访问目标 getter

标签 c# automapper

我有一个忽略目标对象中所有未映射项的映射,但由于某种原因它正在访问映射目标项的 getter,然后抛出空值异常。我的 map 如下

Mapper.CreateMap<A, Entities.B>()
.IgnoreAllUnmapped()
.ForMember(d => d.Registration, opt => opt.MapFrom(s => s.Registration))
.ForMember(d => d.VIN, opt => opt.MapFrom(s => s.Vin))
.ForMember(d => d.MonthReg, opt => opt.MapFrom(s => s.MonthOfRegistration))
.ForMember(d => d.YearReg, opt => opt.MapFrom(s => s.YearOfRegistration))
.ForMember(d => d.BodyColour, opt => opt.MapFrom(s => s.BodyColour));

它在该项目获取方法中的 d.MonthReg 上抛出空值异常,但它不应该在我尝试设置它时调用它。

我该如何解决这个问题?

这是一个代码片段:

public class A
{
    public string Vin { get; set; }
    public string Registration { get; set; }
    public int? MonthOfRegistration { get; set; }
    public int? YearOfRegistration { get; set; }
    public string BodyColour { get; set; }
}

public class Entities.B
{
    public string VIN
    {
      get
      {
        return this.Attributes["VIN"].Value as string;
      }
      set
      {
        this.Attributes["VIN"].Value = (object) value;
      }
    }
    public string Registration
    {
      get
      {
        return this.Attributes["Registration"].Value as string;
      }
      set
      {
        this.Attributes["Registration"].Value = (object) value;
      }
    }
    public int MonthReg
    {
      get
      {
        return (int) this.Attributes["MonthReg"].Value;
      }
      set
      {
        this.Attributes["MonthReg"].Value = (object) value;
      }
    }
    public int YearReg
    {
      get
      {
        return (int) this.Attributes["YearReg"].Value;
      }
      set
      {
        this.Attributes["YearReg"].Value = (object) value;
      }
    }
    public string BodyColour
    {
      get
      {
        return this.Attributes["BodyColour"].Value as string;
      }
      set
      {
        this.Attributes["BodyColour"].Value = (object) value;
      }
    }
}

抛出错误

Test method TestMappingV1 threw exception: 
AutoMapper.AutoMapperMappingException: 

Mapping types:
A-> B
A -> B

Destination path:
B

Source value:
A ---> System.NullReferenceException: Object reference not set to an instance of an object.

at B.get_MonthReg() in source file
at lambda_method(Closure, Object)
at AutoMapper.Impl.PropertyGetter.GetValue(Object source)
at AutoMapper.PropertyMap.GetDestinationValue(Object mappedObject)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)

正在运行的代码是

Mapper.CreateMap<Vehicle, Entities.Vehicle>()
.IgnoreAllUnmapped()
.ForMember(d => d.Registration, opt => opt.MapFrom(s => s.Registration))
.ForMember(d => d.VIN, opt => opt.MapFrom(s => s.Vin))
.ForMember(d => d.MonthReg, opt => opt.MapFrom(s => s.MonthOfRegistration))
.ForMember(d => d.YearReg, opt => opt.MapFrom(s => s.YearOfRegistration))
.ForMember(d => d.BodyColour, opt => opt.MapFrom(s => s.BodyColour));

Mapper.CreateMap<Assessment, Entities.Assessment>()
.IgnoreAllUnmapped()
.BeforeMap((s, d) =>
{
    if (d.IsNull.State)
    {
        d.State = Entities.AssessmentStateEnum.Uninitialised;
    }
})
.ForMember(d => d.AssessmentNumber, opt => opt.MapFrom(s => s.AssessmentNumber))
.ForMember(d => d.State, opt => opt.MapFrom(s => s.State))
.ForMember(d => d.Vehicle, opt => opt.MapFrom(s => s.Vehicle != null ? Mapper.Map<A, Entities.B>(s.Vehicle) : null))    


[TestMethod]
public void TestMappingV1()
{
    Interface.v1.Assessment assessment =
        new Interface.v1.Assessment
        {
            AssessmentNumber = "KDCTEST",
            State = "2",

            Vehicle = new A
            {
                Registration = "ET53 LCO",
                Vin = "VIN123456789",
                MonthOfRegistration = 12,
                YearOfRegistration = 2013
            }
        };

        Mapper.Map(assessment, _assessment);

}

public static IMappingExpression<TSource, TDest> IgnoreAllUnmapped<TSource, TDest>(
this IMappingExpression<TSource, TDest> expression)
{
    expression.ForAllMembers(opt => opt.Ignore());
    return expression;
}

最佳答案

根据 same question在 Github 上,似乎找不到解决此类问题的方法。

Quoting图书馆作者:

Don't think I can get to this one, looking at the source code it seems that the appropriate action would be to have getters that don't throw.

关于c# - 自动映射器 3 访问目标 getter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22453072/

相关文章:

c# - WCF 和可能的 Skype 的端口 80 问题

c# - Html 表,其中每一行都有一个复选框,想要将所有复选框值发送到 ajax 调用

c# - 如何使用 AutoMapper 将父对象映射到子对象?

c# - 避免构造函数映射字段

c# - automapper 无法解析方法 formember

c# - 如果未找到结果,则从 IEnumerable 方法返回 NULL

c# - 使用 AutoMapper 将数据从 SuperClass 复制到 SubClass

c# - 无法在此系统上找到自定义工具,即使它在那里

C# 同时重写和新建

c# - ASP.NET MVC 2 问题 - 路由中的点