PropertyInfo from Delegate

2019-07-19 14:45发布

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?
}

1条回答
对你真心纯属浪费
2楼-- · 2019-07-19 15:10

Using Expression you can:

    static PropertyInfo ExtractProperty<T>(Expression<Func<T>> selector)
    {
        return (selector.Body as MemberExpression).Member as PropertyInfo;
    }
查看更多
登录 后发表回答