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...
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.