Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a list of class/object properties 3

Status
Not open for further replies.

rickinger

Programmer
Feb 11, 2004
20
NL
Is there a way to get a list of all properties a class or an object has during runtime? I'm looking for sort of a collection object.properties[index].count etc...
 
OOps forgot to mention... I mean a list of the actual properties ("field names"), not any values they might contain.
Thanks a lot in advance.
 
Use the GetType() method on your class, which returns you an object of type Type (every object inherits this from the ultimate base class -- the Object class.

You can then call GetProperties(), GetMethods(), etc. to return you an array of objects.

For example:
Code:
Type myType = YourClassInstance.GetType();

MethodInfo[] myMethods = myType.GetMethods(BindingFlags.Public);

for (int i = 0; i < myMethods.Length; i++)
{
   MethodInfo mi = (MethodInfo)myMethods[i];
   Console.WriteLine("MethodName is: {0}", mi.Name;
}

This technique is called Reflection, and is a very powerful part of the .NET framework. It's how the IDE is able to display variable contents, etc. for you in the inspector windows.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks chiph.
I also found GetProperties() which even seems to get a little closer to the actual properties only.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top