C # VS 2005: How to get a list of public class members at runtime?

I am trying to get a list of variables of the memeber class at runtime. I know this, probably using typeof and reflection. but cannot find an example. Please someone shed light for me.

Here is an example of pseudocode:

Class Test01
{ 
 public string str01;
 public string str02;
 public int myint01;
}

I need something like this (pseudocode):

Test01 tt = new Test01();
foreach(variable v in tt.PublicVariableList)
{
   debug.print v.name;
   debug.print v.type;
}

Please help me figure out how to do this in C # VS2005

Thank you so much

+2
source share
2 answers

If you use public fields just use tt.GetType().GetFields()

If you need other participants, use GetProperties () , GetMethods () , GetEvents () , etc. for specific, or GetMembers () to get them all.

, BindingFlags, ( ).

+9
foreach (MemberInfo mi in tt.GetType().GetMembers()) ...
+1

All Articles