C # Reflection Tree

I am trying to find something similar to treeview built into Visual Studio that allows you to navigate through a class. Is there a base library / class that basically contains a tree with mirrored data that iterates through the class and its subclasses? I want a code, I'm not interested in individual applications.

I do not think that it would be difficult to implement with reflection, but I hope someone else did it.

+5
source share
3 answers

If you just want iteration through a nested class, here is an example

    public Form1()
    {
        InitializeComponent();

        Assembly assembly = Assembly.GetAssembly(typeof (DateTime));
        foreach (var exportedType in assembly.GetExportedTypes())
        {
            var parentNode = treeView1.Nodes.Add(exportedType.Name);
            AddNodes(exportedType, parentNode);
        }
    }

    private void AddNodes(Type type,TreeNode node)
    {
        foreach (var nestedType in type.GetNestedTypes())
        {
            var nestedNode = node.Nodes.Add(nestedType.Name);
            AddNodes(nestedType, nestedNode);
        }
    }

Perhaps you also need information on methods, properties etc, in which case you can use

    type.GetProperties();
    type.GetMethods();
    type.GetMembers();
    type.GetEvents();
    type.GetInterfaces();
+2
source

, , , , .Net. .Net , , . , , .

red-gate , . , , ILSpy , SharpDevelop.

ILSpy: ILSpy

+2

. ? , , :

    Type t = typeof(System.Nullable);
    System.Reflection.Assembly a = System.Reflection.Assembly.GetAssembly("System.DLL");
    Type[] types = a.GetTypes();
    foreach (Type type in types)
    {
        if (type.IsSubclassOf(t))
            Console.Write(type.ToString());
    }

Nullable System.DLL. , ,

a = System.Reflection.Assembly.GetExecutingAssembly()

Type . Assembly.

+1

All Articles