循环遍历 poco 类属性
本文关键字:属性 poco 遍历 循环 | 更新日期: 2023-09-27 18:32:29
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;
}
}
有没有办法创建一个嵌套的foreach,获取每个容器名称,以及一个获取每个属性名称+值的inne循环?
希望你明白这个想法,否则不详细,谢谢!
是的,像这样:
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));
}
}
现场示例:http://rextester.com/PNV12550