C# 反射树

发布于 2024-10-20 09:31:05 字数 151 浏览 1 评论 0原文

我试图找到类似于 Visual Studio 中内置的树视图的东西,可以让您遍历一个类。是否有一个基本的库/类,它基本上包含一个带有反射数据的树,该数据遍历一个类及其子类?我想要代码,我对单独的应用程序不感兴趣,

我认为用反射来实现不会那么困难,但我希望其他人已经做到了。

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.

I don't think it would be that difficult to implement with reflection, but I'm hoping somebody else has already done it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅笑依然 2024-10-27 09:31:05

如果您只想迭代嵌套类,这里是一个示例

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

,也许您还需要一些有关方法、属性等的信息,在这种情况下您可以使用

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

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 e.t.c in that case you can use

    type.GetProperties();
    type.GetMethods();
    type.GetMembers();
    type.GetEvents();
    type.GetInterfaces();
终止放荡 2024-10-27 09:31:05

我知道您说过您需要代码,但让我首先提到一个名为 .Net Reflector 的工具。直到最近,它还是每个 .Net 开发人员完成您所谈论的事情的首选工具。几年前它被红门接管,他们最近表示将开始收费。

由于 red-gate 将对反射器收费,因此不少开源项目正在加紧开发替代品。如果我必须预测未来,我会说 ILSpy 成功的机会最大,因为它是由 SharpDevelop 团队推出的。

ILSpy:ILSpy 信息页面

I know that you stated that you want code, but let me start by mentioning a tool called .Net reflector. Until recently, it was the go-to tool for every .Net developer for doing what you are talking about. 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: ILSpy Info Page

给妤﹃绝世温柔 2024-10-27 09:31:05

搜索子类时,首先需要定义边界。您是否在特定程序集中搜索子类?如果是,那么这就是您需要的代码:

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

在上面的代码中,它在 System.DLL 中搜索 Nullable 的所有子类。要获取当前程序集中的子类,只需使用

a = System.Reflection.Assembly.GetExecutingAssembly()

Type 类提供大量信息即可获取当前程序集。 Assembly 类也是如此。

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. 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. So does the Assembly class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文