AutoMapper: After 4.2 ResolveUsing can't handle nulls.

Sorry, I can’t get this to format everything as a code block, or even bolded. If you run this below in a console app you will see the error. There was another work item that seeming had to do with nullable of T. This is unrelated to that issue.

**Mapping types:
Object -> DestinationInner
System.Object -> ConsoleApplication2.Program+DestinationInner

Destination path:
Destination.Inner.Inner

Source value:
(null)
   at AutoMapper.MappingEngine.Map(ResolutionContext context)
   at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy
MapPropertyValue(ResolutionContext context, Object mappedObject, PropertyMap pr
pertyMap)**

`static void Main(string[] args)
{
    var mapper = new AutoMapper.MapperConfiguration(cfg =>
    {
        cfg.CreateMap<Source, Destination>()
            .ForMember(dest => dest.Inner, opt => opt.ResolveUsing(src => src.Inner));
        cfg.CreateMap<SourceInner, DestinationInner>();
    }).CreateMapper();

    var goodSource = new Source()
    {
        Inner = new SourceInner() { Name = "BillRob", },
    };
    var goodDestination = mapper.Map<Source, Destination>(goodSource);
    Console.WriteLine(goodDestination.Inner.Name);

    var nullSource = new Source();
    var nullDestination = mapper.Map<Source, Destination>(nullSource);
    //exception thrown.
}

public class Source
{
    public Source()
    {               
    }

    public SourceInner Inner { get; set; }
}

public class SourceInner
{
    public string Name { get; set; }
}

public class Destination
{
    public DestinationInner Inner { get; set; }
}

public class DestinationInner
{
    public string Name { get; set; }
}

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 17 (8 by maintainers)

Most upvoted comments

And I’m just waiting on .NET Core RC2 to drop.

SOON.gif

On Mon, May 9, 2016 at 3:43 PM, Mac notifications@github.com wrote:

@lbargaoanu https://github.com/lbargaoanu I can’t get any update from https://www.myget.org/F/automapperdev/api/v2. So I can’t wait for the official (or beta) release to be out! [image: 😉]

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/AutoMapper/AutoMapper/issues/1123#issuecomment-217867456