[英]Is there any way to get the PropertyInfo from the getter of that property?
Is there any way I can get the PropertyInfo
for a property from its getter? 有什么办法可以从getter获取
PropertyInfo
吗? Like this: 像这样:
public object Foo
{
get
{
PropertyInfo propertyInfoForFoo = xxx;
...
}
}
I want to avoid having to hard code the name of the property as a string, as that's tricky to maintain. 我想避免将属性的名称硬编码为字符串,因为维护起来很棘手。
I'm using .NET 2.0, so I'm hoping for a linq-less solution. 我正在使用.NET 2.0,因此我希望能够使用无linq解决方案。
MethodBase.GetCurrentMethod() will return the MethodInfo object for get_YourPropertyName. MethodBase.GetCurrentMethod()将返回get_YourPropertyName的MethodInfo对象。
PropertyInfo property = GetType()
.GetProperty(MethodBase
.GetCurrentMethod()
.Name
.Substring("get_".Length)
);
Um... So how were you planning on being "given" the getter? 嗯......那么你打算如何“给予”吸气剂?
The only way I see is by something like MyGetProperyInfo(x => x.Foo);
我看到的唯一方法是通过
MyGetProperyInfo(x => x.Foo);
which can be done, although it requires a lambda & an Expression<> (neither of which is available in C# v2) 这可以做到,虽然它需要一个lambda和一个Expression <>(C#v2中都没有这个)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.