-2

I have to iterate through all members of a class and display them in a Tree. The Tree-Handling can be ignored for now, at this point I'd be satisfied with just getting all class members in a simple list.

All the examples I could find (for instance Recursively Get Properties & Child Properties Of A Class) only return the first two members of the following class cTest:

public class cTest
{
    public String str1;
    public int intwert1;
    public cParent Parent = new cParent();
}

public class cParent
{
    public String parentStr1;
}

The field public cParent Parent is not found and not processed recursively. I could already tell that for the following code:

Type t = typeof(cTest);
PropertyInfo[] propertyInfos;
MemberInfo[] propertyMembers;
propertyInfos = t.GetProperties();
propertyMembers = t.GetMembers();

...propertyInfos does not contain public cParent Parent, but propertyMembers does contain public cParent Parent!

edit

thx, but still stuck. The following code gives the same informations for all class members

        foreach (FieldInfo p in t.GetFields())
        {
            Console.WriteLine("-");
            Console.WriteLine(p.GetType().ToString());
            Console.WriteLine(p.Name.ToString());
            Console.WriteLine(p.MemberType.GetType().ToString());
            Console.WriteLine(p.MemberType.GetTypeCode().ToString());
            Console.WriteLine(p.MemberType.ToString());
        }  

output: ^

System.Reflection.RtFieldInfo
str1
System.Reflection.MemberTypes
Int32
Field
-
System.Reflection.RtFieldInfo
int1
System.Reflection.MemberTypes
Int32
Field
-
System.Reflection.RtFieldInfo
Parent
System.Reflection.MemberTypes
Int32
Field
Community
  • 1
  • 1
Aritex
  • 23

1 Answers1

0

GetProperties() returns the properties of a class. Parent isn't a property, it's a field. GetMembers() returns all members (properties, fields, methods, etc) so that's why it shows up in propertyMembers. Instead of GetProperties(), try using GetFields().

Edit in response to additional questions:

The code you posted is correctly giving the name for each field. The fact that it's returning System.Reflection.MemberTypes and Int32 for every field is because you're not calling the GetType() and GetTypeCode() on the field's type. You're calling them on the FieldInfo.MemberType property, which is an enumeration of the type System.Reflection.MemberTypes (which is an Int32 enum), and as such is the same for all fields.

You need to call those methods on the p.FieldType property, which returns the field's type. Not on the p.MemberType property, which returns wether the member is a method, field, property, etc.

So your code should be something like this:

foreach (FieldInfo p in t.GetFields())
{
    Console.WriteLine("-");
    Console.WriteLine(p.GetType().ToString());
    Console.WriteLine(p.Name.ToString());
    Console.WriteLine(p.FieldType.GetType().ToString());
    Console.WriteLine(p.FieldType.GetTypeCode().ToString());
    Console.WriteLine(p.FieldType.ToString());
}  
Bas
  • 1,946
  • 21
  • 38