从类C#创建嵌套列表

时间:2019-02-23 08:02:52

标签: c#

我要创建像treeView这样的权限嵌套列表,并创建具有两个子类的此类:

 public class Stuff
    {
        public const string _ = "Stuff.";
        public class Invoice
        {
            public const string __ = _ + "Invoice.";
            public const string Add = __ + "Add";
            public const string Edit = __ + "Edit";

        }
        public class Warehouse
        {
            public const string __ = _ + "Warehouse.";
            public const string List = __ + "List";
        }
    }

现在想要这样(TreeView或仅输出无关紧要):

Stuff
  |Invoice
      -Add
      -Edit
  |Warehouse
      -List

我该怎么做?

1 个答案:

答案 0 :(得分:0)

static void Main(string[] args)
    {
           Recoursive(typeof(Permissions).GetNestedTypes());
           Console.ReadLine();
    }


    private static void Recoursive(Type[] type)
    {

            foreach (var c in type)
            {
                Recoursive(c.GetNestedTypes());
                Console.WriteLine(c.Name);
                foreach (var f in c.GetFields())
                {
                    Console.WriteLine("\t-" + f.Name + ":" + f.GetValue(f));
                }
            }
    }

感谢@shingo