简体   繁体   English

C#反射树

[英]C# Reflection Tree

I"m try to find something similar to the treeview built into Visual Studio that lets you traverse a class. Is there a basic library/class that basically contains a tree with Reflected data that iterates through a class and its subclasses? I want code, I'm not interested in separate applications. 我正在尝试寻找类似于Visual Studio内置的树视图的类,它使您可以遍历一个类。是否有一个基本的库/类,该类基本上包含一棵带有反射数据的树,该树遍历一个类及其子类?我想要代码,我对单独的应用程序不感兴趣。

I don't think it would be that difficult to implement with reflection, but I'm hoping somebody else has already done it. 我认为通过反射实现起来不会那么困难,但是我希望其他人已经做到了。

I know that you stated that you want code, but let me start by mentioning a tool called .Net reflector. 我知道您说过要使用代码,但让我首先介绍一个名为.Net反射器的工具。 Until recently, it was the go-to tool for every .Net developer for doing what you are talking about. 直到最近,它仍然是每个.Net开发人员执行您所谈论的内容的必备工具。 It was taken over by red-gate a few years back, and they recently stated that they are going to start charging for it. 几年前,它被红门公司接管,最近他们表示将开始为此收费。

Because red-gate is going to charge for reflector, quite a few open source projects are ramping up development for a replacement. 由于红门将为反射器收费,因此许多开源项目正在加快开发替代品的速度。 If I had to predict the future, I'd say that ILSpy has the best chance of succeeding because its being put out by the SharpDevelop team. 如果我必须预测未来,我会说ILSpy最有可能成功,因为它是由SharpDevelop团队推出的。

ILSpy: ILSpy Info Page ILSpy: ILSpy信息页面

If you just want ot iterate through 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);
        }
    }

Maybe you also want some info about Methods , Properties etc in that case you can use 在这种情况下,也许您还需要有关方法,属性等的一些信息,就可以使用

    type.GetProperties();
    type.GetMethods();
    type.GetMembers();
    type.GetEvents();
    type.GetInterfaces();

When searching for subclasses, first thing you need is to define boundaries. 在搜索子类时,首先需要定义边界。 Are you searching for subclasses within a specific assembly? 您要在特定程序集中搜索子类吗? If yes, then this is the code you'll need: 如果是,那么这是您需要的代码:

    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());
    }

In the code above, it searches for all subclasses of Nullable in System.DLL. 在上面的代码中,它在System.DLL中搜索Nullable的所有子类。 To get the subclasses within the current assembly, simply get the current assembly using 要获取当前程序集中的子类,只需使用以下命令获取当前程序集

a = System.Reflection.Assembly.GetExecutingAssembly()

The Type class provides a lot of information. Type类提供了大量信息。 So does the Assembly class. Assembly课也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM