470,833 Members | 1,350 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

Get name of current property by reflection

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().Na me 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

May 29 '06 #1
1 7871
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/lib...ndmembers.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.)


May 30 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Rakesh | last post: by
15 posts views Thread by Mark Kamoski | last post: by
4 posts views Thread by Bill Borg | last post: by
11 posts views Thread by Alexander Walker | last post: by
8 posts views Thread by news.microsoft.com | last post: by
7 posts views Thread by =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post: by
9 posts views Thread by =?Utf-8?B?VmljdG9y?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.