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...