Scrolling through poco class properties

public partial class Foo
{
    public struct ContainerOne
    {
        public const long Sub1 = 1;
        public const long Sub2 = 2;
    }

    public struct ContainerTwo
    {
        public const long Sub3 = 3;
        public const long Sub4 = 4;
    }

}

Is there a way to make a nested foreach that gets every container name, with an inne loop that gets every property name + value?

I hope you understand this idea, otherwise poorly developed, thanks!

+3
source share
1 answer

Yes, like this:

var fooType = typeof(Foo);
foreach(var type in fooType.GetNestedTypes())
{
    Console.WriteLine(type.Name);
    foreach(var field in type.GetFields())
    {
        Console.WriteLine("{0} = {1}",field.Name,field.GetValue(null));
    }
}

Real-time example: http://rextester.com/PNV12550

+5
source

All Articles