I have a static class that contains a lot of static classes. Each inner static class contains fields. I want to get all fields of all inner static classes.
public static class MyClass
{
public static class MyInnerClass1
{
public const string Field1 = "abc";
public const string Field2 = "def";
public const string Field3 = "ghi";
}
public static class MyInnerClass2
{
public const int Field1 = 1;
public const int Field2 = 2;
public const int Field3 = 3;
}
...
}
I would like to print out the name of each inner class followed by the name and value of each field.
For example:
MyInnerClass
Field1 = "abc"
...
I have no problem with getting the name of all the classes:
var members = typeof(MyClass).GetMembers(BindingFlags.Public | BindingFlags.Static);
var str = "";
foreach (var member in members)
{
str += member.Name +" ";
}
Or the name and value of all fields in a specific class:
var fields = typeof(MyClass.MyInnerClass1).GetFields();
foreach (var field in fields)
{
str += field.Name + "-";
str += field.GetValue(typeof(MyClass.MyInnerClass1));
}
But how do I combine this? The names and the number of inner static classes may change.