jeudi 27 octobre 2016

AutoMapper ignore member failing when mapping with Type objects

Using AutoMapper, I'm trying to map two types that I pull from two different assemblies via reflection. However, when working with Types, I am unable to get the .ForMember() method to ignore any members that I specify. The regular lambda syntax does not compile because the member name is not found on the Type type of the class. Passing in a string of the member allows compilation, but still does not ignore the member.

class ExampleSchema
{
    int Age {get; set;}
}

class ExampleDto
{
    int Age {get; set;}
    int Weight {get; set;}
}

var schemaType = typeof(ExampleSchema);
var dtoType = typeof(ExampleDto);

// This will throw
// Cannot convert lambda expression to string because it is not a delegate type
cfg.CreateMap(schemaType, dtoType)
    .ForMember(dest => dest., opt => opt.Ignore());

// Hard-code cringing aside, this still does not filter out the member
cfg.CreateMap(schemaType, dtoType)
    .ForMember("Weight", opt => opt.Ignore());  

Is this a bug in AutoMapper or am I just using the method incorrectly?





Aucun commentaire:

Enregistrer un commentaire