获取在Nodejs中扩展类的所有类
因此,基本上我有一个代码,该类别
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道一种通过反思或类似技术来做到这一点的方法。
您可以编写一个在文件上迭代的小预处理器,寻找
/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.