Created
October 8, 2017 06:07
-
-
Save lbargaoanu/13459f7e3e353c02b0ae0beac69a0f5a to your computer and use it in GitHub Desktop.
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
namespace Source | |
{ | |
public class Instance | |
{ | |
public Type Type { get; set; } | |
public object Definition { get; set; } | |
} | |
public sealed class Class : Instance | |
{ | |
private IList<Member> _properties; | |
public IList<Member> Properties | |
{ | |
get { return _properties ?? (_properties = new List<Member>()); } | |
set { _properties = value; } | |
} | |
} | |
public abstract class Member : Instance | |
{ | |
public string Name { get; set; } | |
} | |
public sealed class Parameter : Member | |
{ | |
public int Position { get; set; } | |
} | |
public sealed class Property : Member | |
{ | |
} | |
public sealed class Field : Member | |
{ | |
} | |
} | |
namespace Target | |
{ | |
public class Instance | |
{ | |
public Type Type { get; set; } | |
public Instance Definition { get; set; } | |
} | |
public sealed class Class : Instance | |
{ | |
public IList<Member> Properties { get; set; } | |
} | |
public abstract class Member : Instance | |
{ | |
public string Name { get; set; } | |
} | |
public sealed class Parameter : Member | |
{ | |
public int Position { get; set; } | |
} | |
public sealed class Property : Member | |
{ | |
} | |
public sealed class Field : Member | |
{ | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var config = new MapperConfiguration(cfg => | |
{ | |
cfg.CreateMap<object, Target.Instance>() | |
.ForMember(d => d.Definition, o => o.Ignore()) | |
.ForMember(d => d.Type, o => o.Ignore()) | |
#if PreserveReferences | |
.PreserveReferences() | |
#endif | |
; | |
cfg.CreateMap<Source.Instance, Target.Instance>() | |
.Include<Source.Class, Target.Class>() | |
.Include<Source.Member, Target.Member>() | |
.Include<Source.Property, Target.Property>() | |
.Include<Source.Parameter, Target.Parameter>() | |
.Include<Source.Field, Target.Field>() | |
#if PreserveReferences | |
.PreserveReferences() | |
#endif | |
; | |
cfg.CreateMap<Source.Member, Target.Member>() | |
.Include<Source.Property, Target.Property>() | |
.Include<Source.Parameter, Target.Parameter>() | |
.Include<Source.Field, Target.Field>() | |
#if PreserveReferences | |
.PreserveReferences() | |
#endif | |
; | |
cfg.CreateMap<Source.Class, Target.Class>() | |
#if PreserveReferences | |
.PreserveReferences() | |
#endif | |
; | |
cfg.CreateMap<Source.Property, Target.Property>() | |
#if PreserveReferences | |
.PreserveReferences() | |
#endif | |
; | |
cfg.CreateMap<Source.Parameter, Target.Parameter>() | |
#if PreserveReferences | |
.PreserveReferences() | |
#endif | |
; | |
cfg.CreateMap<Source.Field, Target.Field>() | |
#if PreserveReferences | |
.PreserveReferences() | |
#endif | |
; | |
}); | |
config.AssertConfigurationIsValid(); | |
var mapper = config.CreateMapper(); | |
// Source | |
var field = new Source.Field() { Name = "AddResult", Type = typeof(Int32) }; | |
var @class = new Source.Class() { Properties = new List<Source.Member> { field }, Type = typeof(float) }; | |
var returnValue = new Source.Instance { Type = typeof(float), Definition = @class }; | |
@class.Definition = @class; | |
var members = new List<Source.Member> { | |
new Source.Property { Name = "(return)", Definition = returnValue.Definition, Type = returnValue.Type } | |
}; | |
var returnValues = mapper.Map<List<Target.Member>>(members); | |
Console.Read(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment