RSS

Get Property Names using Reflection in C#

Sun, Dec 14, 2008

Programming, Tutorials

This example shows how we can get names of all properties for a specific type by using Type.GetProperties.

Example

 
PropertyInfo[] propertyInfoCollection;
propertyInfoCollection = typeof(MyTestClass).GetProperties(BindingFlags.Public |
                                              BindingFlags.Static);
// sorting property
Array.Sort(propertyInfoCollection,
        delegate(PropertyInfo myPropertyInfo1, PropertyInfo myPropertyInfo2)
        { return myPropertyInfo1.Name.CompareTo(myPropertyInfo2.Name); });
 
// Displaying property names by iterating through collection
foreach (PropertyInfo property in propertyInfoCollection)
{
  Console.WriteLine(property.Name);
}
Sharing ~ Helping Other:
  • Print
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • BlinkList
  • DZone
  • Slashdot
  • YahooMyWeb
  • StumbleUpon
  • Live
  • IndianPad
  • DotNetKicks
  • Technorati

Related Posts:

, ,

This post was written by:

eXclusiveMinds - who has written 500 posts on eXclusiveMinds.


Contact the author

Leave a Reply

You must be logged in to post a comment.