Skip to content

Instantly share code, notes, and snippets.

@lbargaoanu
Created March 2, 2016 12:31
Show Gist options
  • Save lbargaoanu/8263a9f001107ac6ae1b to your computer and use it in GitHub Desktop.
Save lbargaoanu/8263a9f001107ac6ae1b to your computer and use it in GitHub Desktop.
Child -> parent conversion issue
using System;
namespace AutoMapper_Sample
{
class Program
{
abstract class DTOParent
{
}
class Sample : DTOParent
{
public int ID { get; set; }
}
class SampleChild : Sample
{
public string Name { get; set; }
}
static void Main(string[] args)
{
var child = new SampleChild()
{
ID = 1,
Name = "Sample"
};
//Bad type - "SampleChild" instead of "Sample"
var sample_new2 = ConvertDTOType_new2(typeof(SampleChild), typeof(Sample), child);
}
private static AutoMapper.IMapper mapper = new AutoMapper.MapperConfiguration(cfg => {
cfg.CreateMap<SampleChild, Sample>();
cfg.CreateMissingTypeMaps = true;
}).CreateMapper();
static DTOParent ConvertDTOType_new2(Type fromType, Type toType, DTOParent convertableDTO)
{
return (DTOParent)mapper.Map(convertableDTO, fromType, toType);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment