嵌套 struct & class 的遍历

  假设有如下的结构或者类,那么我们怎么遍历它们呢?

    public  struct AAAAA
    {
        public  struct BBBBB
        {
            public struct CCCCC
            {
                public static string ZZ = "ZZ";
                public struct DDDDD
                {
                    public static string ZZ = "ZZ";
                }
            }

        }
    }

  遍历代码参考:

        static void A777()
        {
            Console.WriteLine(AAAAA.BBBBB.CCCCC.ZZ);
            Console.WriteLine(typeof(AAAAA).FullName);
            Any(typeof(AAAAA));

        }

        static void Any(Type sub)
        {
            foreach (var m in sub.GetMembers(System.Reflection.BindingFlags.Public))
            {
                Console.WriteLine($"Member:{m.Name}=>>{m.ReflectedType.FullName}");
            }
            foreach (var t in sub.GetNestedTypes())
            {
                Console.WriteLine($"struct:{t.Name}=>>{t.FullName}");
                Any(t);
            }
        }

  结果:

ZZ
ConsoleApp1.AAAAA
Member:BBBBB=>>ConsoleApp1.AAAAA
struct:BBBBB=>>ConsoleApp1.AAAAA+BBBBB
Member:CCCCC=>>ConsoleApp1.AAAAA+BBBBB
struct:CCCCC=>>ConsoleApp1.AAAAA+BBBBB+CCCCC
Member:DDDDD=>>ConsoleApp1.AAAAA+BBBBB+CCCCC
struct:DDDDD=>>ConsoleApp1.AAAAA+BBBBB+CCCCC+DDDDD

  

posted @ 2020-05-07 14:07  devs  阅读(219)  评论(0编辑  收藏  举报