开发者

Is there anyway to programatically discern which property code is executing within?

I wish to call something similar to

public static void Foo()
{
    PropertyInfo prop = xxx;
} 

from

public string Bar()
{
   get { return Foo(); }
}

I want prop to be the PropertyInfo for the calling property, I am at a loss as to what xxx would be.

Any ide开发者_开发百科as folks?


public string Bar
{
    get { return Foo(GetType().GetProperty("Bar")); }
}


A property is in reality two methods: get_PropertyName and set_PropertyName. You can get these method names using the StackTrace class:

public string MethodName
{
  get { return new StackTrace(true).GetFrame(0).GetMethod().Name.Substring(4); }
}

The Substring call removed the get_ part of the method name so you get the property name only.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜