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)
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: