I use the following code to extract named members from an anonymous type. Is there a way to convert the follwing code to use a lambda expression to achieve this, or at least allow the calling code to use lamda, even if I need to use the string "deep down"?
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});
This new {NewJobCard = model} is what GetAnonymousTypeMember is GetAnonymousTypeMember as anonymousType .
source share