开发者

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

开发者 https://www.devze.com 2023-01-11 19:17 出处:网络
Is there any way I can get the PropertyInfo for a property from its getter? Like this: public object Foo

Is there any way I can get the PropertyInfo for a property from its getter? 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.


MethodBase.GetCurrentMethod() will return the MethodInfo object for get_YourPropertyName.

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);

which can be done, although it requires a lambda & an Expression<> (neither of which is available in C# v2)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号