来自Delegate的PropertyInfo

时间:2010-04-28 01:00:48

标签: c# reflection delegates

是否有一种简单的方法来获取委托中属性的PropertyInfo,假设它是一个简单的属性seletor?

示例:

var propertyInfo = Method<MyClass,int>(s => s.Property);

...

PropertyInfo Method(Func<T1,T2> selector)
{
   // What goes here?
}

1 个答案:

答案 0 :(得分:8)

使用Expression您可以:

    static PropertyInfo ExtractProperty<T>(Expression<Func<T>> selector)
    {
        return (selector.Body as MemberExpression).Member as PropertyInfo;
    }