views:

41

answers:

1

I'd like to generalize a property lookup from code like this

.ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.GetValue("FirstName"))
.ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.GetValue("LastName"))
... (repeated for many properties)

to a one-liner, conceptually something like this:

// How can I access the property name?
.ForAllMembers(opt => opt.MapFrom(src => src.GetValue([[PROPERTYNAME]]))   

The "source" value is almost always be a string-based lookup into a GetValue() method, using the property name from the destination. I just don't know how to access the string name of the property from the "source" lambda when it's defined in the "destination" lambda. It seems like there ought to be a way to do this but I'm not having luck finding a relevant example.

I hope this makes sense. Thanks in advance for any insights,

Jeff

+1  A: 

You should be able to use a Custom Resolver that uses reflection to get all the property names and then calls your GetValue() method of the source object.

Jeff T