繁体   English   中英

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

[英]Is there any way to get the PropertyInfo from the getter of that property?

有什么办法可以从getter获取PropertyInfo吗? 像这样:

public object Foo
{
    get
    {
        PropertyInfo propertyInfoForFoo = xxx;
        ...
    }
}

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

我正在使用.NET 2.0,因此我希望能够使用无linq解决方案。

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

PropertyInfo property = GetType()
                            .GetProperty(MethodBase
                                             .GetCurrentMethod()
                                             .Name
                                             .Substring("get_".Length)
                                        );

嗯......那么你打算如何“给予”吸气剂?

我看到的唯一方法是通过MyGetProperyInfo(x => x.Foo);

这可以做到,虽然它需要一个lambda和一个Expression <>(C#v2中都没有这个)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM