I’m using the following code to retrieve named members from an anonymous type. Is there some way I could convert the follwing code to use a lambda expression to achieve this, or at least to allow the calling code to use a lamda, even if ‘deep down’ I have to use a string?
private T GetAnonymousTypeMember<T>(object anonymousType, string memberName) where T: class
{
var anonTypesType = anonymousType.GetType();
var propInfo = anonTypesType.GetProperty(memberName);
return propInfo.GetValue(anonymousType, null) as T;
}
ADDED:
This is how anonymousType arrives. The GetAnonymousTypeMember method is private to a class whose only public method is declared as follows:
public void PublishNotification(NotificationTriggers trigger, object templateParameters)
I call this method:
PublishNotification(NotificationTriggers.RequestCreated, new {NewJobCard = model});
That new {NewJobCard = model} is what is passed to GetAnonymousTypeMember as anonymousType.
Will enable to to do: