Skip to content

AutoMapper 6.0 can't map nullable types containing null when not built using .NET Native tool chain #2016

@mobilemotion

Description

@mobilemotion

Mapping an object that contains a nullable property which contains null throws an AutoMapperMappingException with the following message:

Error mapping types.

Mapping types:
In -> Out
Workheld.Core.In -> Workheld.Core.Out

Type Map configuration:
In -> Out
Workheld.Core.In -> Workheld.Core.Out

Property:
B

The inner exception says

Non-static method requires a target.

This happens only when building in Debug mode, meaning when the Compile with .NET Native tool chain compiler option is not checked. When using .NET Native tool chain, the mapping works and produces the expected result. Also, when using AutoMapper 5.1.1, the mapping works.

To reproduce, create a Portable Class Library targeting:

  • .NET Framework 4.5
  • Windows 8
  • APS.NET Core 1.0
  • Windows Phone 8.1
  • Xamarin.Android
  • Xamarin.iOS
  • Xamarin.iOS (Classic)
  • Xamarin.Mac

(Profile111) that references the AutoMapper 6.0.1 NuGet package, and add the following classes:

public class In
{
    public int? A { get; set; }
    public int? B { get; set; }
}
public class Out
{
    public int? A { get; set; }
    public int? B { get; set; }
}
public static class TestClass
{
    public static Out TestMethod()
    {
        Mapper.Initialize(c =>
        {
            c.CreateMap<In, Out>();
        });
        var input = new In
        {
            A = 123
        };
        return Mapper.Map<In, Out>(input);
    }
}

Then, create a blank UWP app project that references the PCL and calls the TestMethod. Run it in Debug and Release mode, and watch the different behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions