是否有一种简单的方法来获取委托中属性的PropertyInfo,假设它是一个简单的属性seletor?
示例:
var propertyInfo = Method<MyClass,int>(s => s.Property);
...
PropertyInfo Method(Func<T1,T2> selector)
{
// What goes here?
}
答案 0 :(得分:8)
使用Expression您可以:
static PropertyInfo ExtractProperty<T>(Expression<Func<T>> selector)
{
return (selector.Body as MemberExpression).Member as PropertyInfo;
}