5

First of all, sorry for my bad English... I hope you'll understand what I want to say.

I have a problem with a little code where I need to get the value of class' properties. (That's not my full project, but the concept of what I want to do. And with this simple code, I'm blocked.)

There is the code: (This sample works correctly.)

using System;
using System.Reflection;

class Example
{
    public static void Main()
    {
        test Group = new test();
        BindingFlags bindingFlags = BindingFlags.Public |
                                    BindingFlags.NonPublic |
                                    BindingFlags.Instance |
                                    BindingFlags.Static;
        Group.sub.a = "allo";
        Group.sub.b = "lol";

        foreach (PropertyInfo property in Group.GetType().GetField("sub").FieldType.GetProperties(bindingFlags))
        {
            string strName = property.Name;
            Console.WriteLine(strName + " = " + property.GetValue(Group.sub, null).ToString());
            Console.WriteLine("---------------");
        }
    }
}

public class test
{
    public test2 sub = new test2();
}

public class test2
{
    public string a { get; set; }
    public string b { get; set; }
}

But I want to replace Group.sub with a dynamic accessing (like the foreach with GetField(Var) where it works). I tried a lot of combinations, but I haven't found any solutions.

property.GetValue(property.DeclaringType, null)

or

property.GetValue(Group.GetType().GetField("sub"), null)

or

property.GetValue(Group.GetType().GetField("sub").FieldType, null)

So I think you understand. I would like to give the instance of object Group.sub dynamically. Because, on my full project, I have a lot of subclasses.

Any ideas?

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Kevin Joss
  • 53
  • 4

1 Answers1

3

You're already accessing the sub field using Group.GetType().GetField("sub"), you'll need to get its value and hold on to it:

FieldInfo subField = Group.GetType().GetField("sub");

// get the value of the "sub" field of the current group
object subValue = subField.GetValue(Group);
foreach (PropertyInfo property in subField.FieldType.GetProperties(bindingFlags))
{
    string strName = property.Name;
    Console.WriteLine(strName + " = " + property.GetValue(subValue, null).ToString());    
}
C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • Thank you C:Evenhuis for you response. It's exactly what i want to do ! I have tested and it works. To do the same with an adding level, can we do like that ? FieldInfo subField = Group.GetType().GetField("sub"); object subValue = subField.GetValue(Group); FieldInfo subField2 = Group.GetType().GetField("sub").FieldType.GetField("sub2"); object subValue2 = subField2.GetValue(subValue); Or a a faster way exists ? – Kevin Joss Jan 05 '17 at 12:10
  • 1
    Yeah you can, and you can "hold on" to previous results - ie. to get the "sub2" field info, you don't have to start at `Group.GetType()...`; you can use `subField.FieldType.GetField("sub2")`. – C.Evenhuis Jan 05 '17 at 13:01
  • Thank you C.Evenhuis. I did it like that =) – Kevin Joss Jan 06 '17 at 08:04