0

I have tried to find a solution for this but I cannot. Given the following classes and Program:

[Serializable]
public class SomeClass
{
    public string OneProp { get; set; }
    public string TwoProp { get; set; }

    public string OneField;
    public string TwoField;
}
[Serializable]
public class Class1
{
    public static string gsFoo { get; set; }
    public static string gsGar { get; set; }

    public static string gsFooField;
    public static string gsGarField;

    public static SomeClass someClass = new SomeClass();  
}

public class Program
{
    private static void PrintValues(Type type)
    {
        foreach (var field in type.GetFields())
        {
            Console.WriteLine("{0}={1}", field.Name, field.GetValue(null));
            var v = field.GetValue(null); 
        }

        foreach (var prop in type.GetProperties())
        {
            Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(type, null));
            var v = prop.GetValue(null,null);
        }
    }

    static void Main(string[] args)
    {
        Class1.gsGar = "gar prop";
        Class1.gsFoo = "foo prop";
        Class1.gsFooField = "foo field";
        Class1.gsGarField = "gar field";

        Class1.someClass.OneField = "1 field";
        Class1.someClass.TwoField = "2 field";

        Class1.someClass.OneProp = "1 prop";
        Class1.someClass.TwoProp = "2 prop";

        PrintValues(typeof(Class1));

        while (true)
        {
        }
    }
}

If I set the properties and fields of Class1.SomeClass, how can I serialize Class1 and see the names/values of SomeClass such that it prints like this:

Class1.SomeClass.OneProp = "1 prop"
Class1.SomeClass.TwoProp = "2 prop"
Class1.SomeClass.OneField = "1 field"
Class1.SomeClass.TwoField = "2 field"

I can get the names/values from FieldInfo and PropertyInfo of other static types that are not custom types using GetType() and GetValue(), but in this case, FieldInfo (if I'm not mistaken) is the metadata on this custom type, but I can't see how to get the member types' names and values that are inside of it.

I hope this makes sense...

  • Show us what you have tried so far, then we can tell you what you did wrong or what your misunderstanding is. –  May 07 '14 at 16:38
  • 2
    An object with fields of a custom type is logically a tree. FieldInfo.FieldType provides the type. A tree is best iterated with recursion. Flattening a tree into something that's easily serializable will set a back for a while, there is nothing very trivial about BinaryFormatter. It has been done before. – Hans Passant May 07 '14 at 17:04
  • When you iterate over the Class1 fields, your code will eventually reach the *someClass* field. All which your code is doing at this point is this: `Console.WriteLine("{0}={1}", "someClass", Class1.someClass)`. Look at that line -- what sort of black magic do you expect *Console.WriteLine* to do? Instead, you should implement and call something like `PrintInstanceValues(object o)` (which has to work on object instances since the properties of SomeClass are not static) when you encounter a field/property with a custom type. –  May 07 '14 at 17:17
  • I think I understand - will give that a try – user2566049 May 07 '14 at 17:20
  • Implement a method like PrintInstanceValues(object o). Its implementation will be very, very similar to your `PrintValues(Type type)` with the distinction that it will work on the actual object instances instead of the Type object (because: non-static fields/properties). –  May 07 '14 at 17:22
  • But when I iterate the fields in .GetFields(), the result is an array of FieldInfo objects that are not castable to any type. How would I iterate over the Class1 fields/properties in order to test whether or not I've encountered a member that is an instance? – user2566049 May 07 '14 at 17:29
  • Please look at the MSDN documentation for GetFields(BindingFlags). It will tell you which BindingFlags to pass to this method for getting instance (non-static) fields or static fields and much more. –  May 07 '14 at 17:34
  • Ok I still don't think I understand after trying. Even though "SomeClass" is marked static, it is still instanced - but it is also static? Should this member appear if I use BindingFlags.Static or BindingFlags.Instance? – user2566049 May 07 '14 at 17:56

0 Answers0