Created
April 10, 2020 07:00
-
-
Save gongdo/5dac34a73421628cc82ad92102995440 to your computer and use it in GitHub Desktop.
Constructor mapping is not working for class but struct.
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 AutoMapper; | |
using System.Diagnostics; | |
namespace ConstructorMappingTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var configuration = new MapperConfiguration(cfg => | |
{ | |
cfg.CreateMap<Source, Destination>(); | |
cfg.CreateMap<Source, DestinationClass>(); | |
}); | |
var mapper = configuration.CreateMapper(); | |
var source = new Source { Property = "value" }; | |
// even readonly struct is ok | |
var prev = new Destination("prev"); | |
var destination = mapper.Map(source, prev); | |
Debug.Assert(destination.Property == "value"); | |
// but class, no luck. | |
var prevClass = new DestinationClass("prev"); | |
var destinationClass = mapper.Map(source, prevClass); | |
Debug.Assert(destinationClass.Property == "value"); | |
} | |
public struct Source | |
{ | |
public string Property { get; set; } | |
} | |
// readonly struct version--------------------------- | |
public readonly struct Destination | |
{ | |
public Destination(string property) | |
{ | |
Property = property; | |
} | |
public string Property { get; } | |
} | |
// class version--------------------------- | |
public class DestinationClass | |
{ | |
public DestinationClass(string property) | |
{ | |
Property = property; | |
} | |
public string Property { get; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment