Created
March 2, 2016 12:31
-
-
Save lbargaoanu/8263a9f001107ac6ae1b to your computer and use it in GitHub Desktop.
Child -> parent conversion issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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