Reputation: 6238
Is there a simple way to get the PropertyInfo for a property in a delegate, assuming it is a simple property seletor?
Example:
var propertyInfo = Method<MyClass,int>(s => s.Property);
...
PropertyInfo Method(Func<T1,T2> selector)
{
// What goes here?
}
Upvotes: 2
Views: 363
Reputation: 3781
Using Expression you can:
static PropertyInfo ExtractProperty<T>(Expression<Func<T>> selector)
{
return (selector.Body as MemberExpression).Member as PropertyInfo;
}
Upvotes: 8