有没有办法从该属性的getter获取PropertyInfo?

Jon*_*n B 7 .net c# reflection properties propertyinfo

有什么方法可以PropertyInfo从吸气剂中获取物业吗?像这样:

public object Foo
{
    get
    {
        PropertyInfo propertyInfoForFoo = xxx;
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我想避免将属性的名称硬编码为字符串,因为维护起来很棘手.

我正在使用.NET 2.0,所以我希望能够使用无linq的解决方案.

Kir*_*oll 5

MethodBase.GetCurrentMethod()将返回get_YourPropertyName的MethodInfo对象.

PropertyInfo property = GetType()
                            .GetProperty(MethodBase
                                             .GetCurrentMethod()
                                             .Name
                                             .Substring("get_".Length)
                                        );
Run Code Online (Sandbox Code Playgroud)