通过混合列出类的方法 [英] Listing a class's methods trough mixin
问题描述
我想在mixin中列出类的方法.
I want to list methods of a class in a mixin.
要做到这一点,我假设我必须打开在mixin中包含源代码的文件,但是要找到该文件的名称比我想象的要难.
To do it I assume I have to open the file containing the source code in the mixin, but finding that file's name is harder than I thought.
我尝试使用
__FILE__
但是它提供了mixin声明的文件...所以我将不得不在每个文件中定义mixin ...这没有任何意义,因为目标是减少样板代码.
but it gives the mixin declaration's file... so I would have to define the mixin in every file... which doesn't make any sense since the goal is to reduce boilerplate code.
现在,我的解决方案是将文件名作为参数传递给类的构造函数,然后从那里调用mixin.由于我将在继承自同一父类的所有类中执行此操作,因此理想情况下,我希望将其放入父类的构造函数中并注册子类的方法.
For now my solution is to pass the filename as an argument inside the class's constructor and call the mixin from there. Since I will do that in all class that inherit from the same parent class, I would ideally want to put it in the parent class's constructor and to register the child class's methods.
是否有更好的方法? 无需打开文件就可以在类中搜索方法的任何内容吗?
Is there a better way of doing this? Anything that could search the class for methods without having to open the file?
推荐答案
这应该是一个不错的起点: http://dlang.org/traits.html#allMembers
This should be a good place to start: http://dlang.org/traits.html#allMembers
然后,您可以使用其他特征来确定哪些成员是方法.
Then you can use the other traits to figure which members are methods.
这篇关于通过混合列出类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!