Reputation: 22770
Is there a way I can do something like this?
public void CreateMap<T, I>(??? ForMember)
{
Mapper.CreateMap<T, I>().ForMember(ForMember);
}
I'm just trying to pass the ForMember as an argument a method that I can then attach to the Map.
Upvotes: 1
Views: 927
Reputation: 14972
Here is a possible way to do it: given two classes
internal class ExampleClass
{
public string Name { get; set; }
}
internal class OtherExampleClass
{
public string OtherName { get; set; }
}
Define the following function
private static void CreateMap<From, To>
(Expression<Func<From, object>> FromExpression,
Expression<Func<To, object>> ToExpression)
{
Mapper.CreateMap<From, To>()
.ForMember(ToExpression, opt => opt.MapFrom(FromExpression));
}
And call it by passing the lambdas directly:
CreateMap<ExampleClass, OtherExampleClass>(fromClass => fromClass.Name, toClass => toClass.OtherName);
The IMappingExpression
(the type returned from the ForMember
method) is not something that you should use. It holds configuration and chaining methods but i don't think it is supposed to come from the outside. With a brief survey of the code i couldn't find any point at which you could add IMappingExpression
so using the two lambdas will be your best bet imo
Upvotes: 2