Reputation: 22857
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 ideas folks?
Upvotes: 1
Views: 79
Reputation: 36300
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.
Upvotes: 2
Reputation: 1038810
public string Bar
{
get { return Foo(GetType().GetProperty("Bar")); }
}
Upvotes: 4