问题
I want to list methods of a class in a 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.
I tried using
__FILE__
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.
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?
回答1:
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.
来源:https://stackoverflow.com/questions/11314596/listing-a-classs-methods-trough-mixin