自动映射器3访问目标getter

时间:2014-03-17 11:25:26

标签: c# automapper

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

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的项目get方法中抛出一个空值异常,但它不应该调用它,因为我正在尝试设置它。

我该如何解决这个问题?

以下是代码段:

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

1 个答案:

答案 0 :(得分:0)

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

Quoting图书馆作者:

  

不要以为我可以到达这个,看看源代码似乎适当的行动就是让不会抛出的吸气剂。

相关问题