0

I realise the question sounds similar to some others, but they don't really answer what I need. I'm trying to use Reflection to put a big "data" class which has other data classes within it and get all these values to quickly create a JSON so that every time a new piece of data is added there is no need to re-code anything.

So, basically it would look something like this:

public class A
{
    public int data1 { set; get; }
    public B b { set; get; }
}
public class B
{
    public int data2 { set; get; }
}

//main method
{
    A a = new A();
    a.data1 = 1;
    a.b = new B();
    a.b.data2 = 2;

    PropertyInfo[] propInfos = typeof(A).GetProperties();
    foreach (PropertyInfo propInfo in propInfos)
    {
        // ??
        //if (!propInfo.GetType().IsPrimitive)
        //{
        //    PropertyInfo[] propInfosForB = propInfo.GetType().GetProperties();
        //    foreach (PropertyInfo propInfoForB in propInfos)
        //    {
        //        print(propInfoForB.GetValue(propInfo, null));
        //    }
        //}
        //else
        {               
            print(propInfo.GetValue(a, null));
        }
    }
}

So basically "1" prints fine, but I can't figure out how to get "2" from B (as part of A). All I get is loads of "1" and "B" printed or errors. Hope that makes sense.

P.S: I know serialization would be a simpler solution to put all this in a JSON, but I can't use that for different reasons.

EDIT: Sorry for the duplication. The comments/replies helped me understand better what I'm doing and what I'm looking for though so thanks!

AlexTudo
  • 54
  • 6
  • 3
    What reasons? Because you are trying to write a Json serializer yourself at this point. Why not use Json.NET ? You'll end up writing the *same* code, only yours will be slower and buggier – Panagiotis Kanavos Aug 19 '16 at 14:06
  • So, you basically want to print out the full object structure? – Icepickle Aug 19 '16 at 14:06
  • your second foreach statement is going over the properties of A again. It should be foreach(PropertyInfo propInfoForB in propInfosForB) – Anil Goel Aug 19 '16 at 14:06
  • Haven't looked too much into this, but it seems you want to recursively go into child objects and there are plenty of examples of how to do this. For example: http://stackoverflow.com/a/20554262/1693085 – John Bustos Aug 19 '16 at 14:07
  • JohnBustos, that's what I was looking for! Thanks very much, I should have used the word "recursive" in my searching. Silly me. PanagiotisKanavos, I was afraid that I might be reinventing the wheel. Honestly, a colleague researched this thoroughly and found various problems with serialising this and using it with PHP and also many "types" from Unity which didn't seem to be supported by this (such as Quaternion, Color, Vector3 etc.) However, we should review those reasons and try serialization again 'cause it's pretty neat. Thanks everyone for all your help and sorry for the duplication! – AlexTudo Aug 19 '16 at 14:57

0 Answers0