获取在Nodejs中扩展类的所有类

发布于 2025-02-06 04:40:33 字数 572 浏览 1 评论 0原文

因此,基本上我有一个代码,该类别

class CommandBase {
    doCommand(){
        // Do command thing.
    }
}

class Help extends CommandBase{

}

class Kick extends CommandBase{
    
}

class Ban extends CommandBase{
    
}

class Chat extends CommandBase{
    
}

现在在整个程序的整个生产过程中扩展了一个基类,将添加越来越多的命令,以扩展命令库类,是否有任何方法可以通过反射我可以得到所有类可以在数组中明确编写它们的课程或通过手动编写代码来跟踪它们?我想在c#中有这样的东西:

AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(s => s.GetTypes())
    .Where(p => type.IsAssignableFrom(p))

nodejs可以这样做吗?有什么特定的反射库吗?

so basically I have a this code where classes extends over a base class

class CommandBase {
    doCommand(){
        // Do command thing.
    }
}

class Help extends CommandBase{

}

class Kick extends CommandBase{
    
}

class Ban extends CommandBase{
    
}

class Chat extends CommandBase{
    
}

now throughout the production of the program more and more commands will be added that extends the CommandBase class, is there any way via reflection I can just get all classes the extends the CommandBase classes without to explicitly write them in an array or keeping track of them by writing manually on code? I want to have something like this in C#:

AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(s => s.GetTypes())
    .Where(p => type.IsAssignableFrom(p))

Can nodeJS do this? is there any specific reflection library for it?

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

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

发布评论

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

评论(1

不语却知心 2025-02-13 04:40:33

我不知道一种通过反思或类似技术来做到这一点的方法。

您可以编写一个在文件上迭代的小预处理器,寻找/class \ s+([\ s]+)\ s+扩展可用于实际代码执行的表单。

I'm not aware of a way to do this with reflections or similar techniques.

You could write a little pre-processor that iterates over your files, looks for /class\s+([\S]+)\s+extends\s+CommandBase/g and stores the results in a form that can be used for the actual code execution.

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