Created
January 10, 2018 11:36
-
-
Save javiercampos/dbf1fb1e17e143e2b693b56711301ee2 to your computer and use it in GitHub Desktop.
Automapper ExpressionBuilder Ctor Parameter Error
This file contains 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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using AutoMapper; | |
using AutoMapper.QueryableExtensions; | |
namespace AutomapperTestCtor | |
{ | |
public class ClassA | |
{ | |
public string PropertyA { get; set; } | |
public string PropertyB { get; set; } | |
} | |
public class ClassB | |
{ | |
public string PropertyA { get; } | |
public string PropertyB { get; } | |
public ClassB(string propertyA, string propertyBWithDifferentName) | |
{ | |
PropertyA = propertyA; | |
PropertyB = propertyBWithDifferentName; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var mapperConfig = new MapperConfiguration(cfg => | |
{ | |
cfg.CreateMap<ClassA, ClassB>() | |
.ForCtorParam("propertyA", x => x.MapFrom(y => y.PropertyA)) | |
.ForCtorParam("propertyBWithDifferentName", x => x.MapFrom(y => y.PropertyB)) | |
; | |
}); | |
mapperConfig.AssertConfigurationIsValid(); | |
var listOfA = new List<ClassA> { new ClassA { PropertyA = "Foo", PropertyB = "Bar" } }; | |
var mapExpression = mapperConfig.ExpressionBuilder.GetMapExpression<ClassA, ClassB>(); | |
foreach (var result in listOfA.Select(mapExpression.Compile())) | |
{ | |
Console.WriteLine(result.PropertyB); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment