Get name of current property by reflection

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I wonder how to get the name of the currently executing property by using
reflection in .Net 2.0

If I use System.Reflection.MethodInfo.GetCurrentMethod().Name from within a
property I get something like "set_MyProperty" instead of "MyProperty". Is
there a way to get the current property name without the "set_" or "get_"
prefix?

Thanks,
Guido
 
Hello Guido,

Welcome to the MSDN newsgroup.

As for the "System.Reflection.MethodInfo.GetCurrentMethod()" method, it
always returns the methodinfo of the current executing function on the
stack. However, an executing function is not necessarily a property,
therefore the "System.Reflection.MethodInfo.GetCurrentMethod()" method is
not suitable for getting property information. Also, in .net framework , a
property is actually represented by two raw accessor methods (get_PROPNAME
and set_PROPNAME), and in some languages such as C++, properties are even
directly defined by two functions. e.g.

==========

public: __property System::Int32 get_CodePage()
{
...................
}
public: __property void set_CodePage(System::Int32 value)
{
..........................
}
================

That's why the "System.Reflection.MethodInfo.GetCurrentMethod()" method can
only return the get/set accessor function name rather than the property
name.

Generally, we're recommended to use the "GetProperties" or "FindMembers"
method of the "Type" class to query property info from a certain class.

#Type.FindMembers Method
http://msdn2.microsoft.com/en-us/library/system.type.findmembers.aspx

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top