简体   繁体   English

如何在Haxe中检索模块中所有类的列表? (又名:助手班)

[英]How to retrieve a list of all classes in a module in Haxe? (aka: helper classes)

Considering a Haxe file defines a series of classes like so: 考虑到Haxe文件定义了一系列类,如下所示:

import flash.display.MovieClip;
import flash.display.Sprite;
import haxe.unit.TestCase;

class MainTest extends Sprite { 
    public var testcase:Array<Class<TestCase>> = ???;
}

class TestSprite extends TestCase {
    function testBasic() {
        var sprite = new Sprite();
        sprite.x = 0;
        assertEquals(sprite.x, 0);
    }
}

class TestMovieClip extends TestCase {
    function testMovieClip() {
        var mc = new MovieClip();
        mc.nextFrame();
        assertEquals(mc.currentFrame, 2);
    }
}

Is there a way to obtain a list of all the helper classes (ex: TestSprite and TestMovieClip )? 有没有办法获取所有帮助程序类的列表(例如: TestSpriteTestMovieClip )? Preferably at runtime, but a macro that would return an Array<Class<TestCase>> would work fine too. 最好在运行时,但是返回Array<Class<TestCase>>的宏也可以正常工作。

I have a small macro helper library called compiletime that can get all classes in a package, or all classes that extend a certain class. 我有一个名为compiletime的小型宏助手库,它可以获取包中的所有类,或者扩展某个类的所有类。

haxelib install compiletime

And then get classes either by base class or by package: 然后通过基类或包来获取类:

var testcases = CompileTime.getAllClasses("my.package");
var testcases = CompileTime.getAllClasses(TestCase);
var testcases = CompileTime.getAllClasses("my.package",TestCase); // Both!

Now, that is getting them by package, not by module. 现在,这是通过包而不是模块获得它们。 Getting it by module might work, I'm not sure off the top of my head. 通过模块获取它可能会起作用,我不确定是不是最重要的。 But if you were to edit this part of the code: 但是如果你要编辑这部分代码:

https://github.com/jasononeil/compiletime/blob/master/src/CompileTime.hx#L208 https://github.com/jasononeil/compiletime/blob/master/src/CompileTime.hx#L208

And change it to also support getting by module, and send me a pull request, I would most certainly merge it :) 并将其更改为也支持按模块获取,并向我发送拉取请求,我肯定会合并它:)

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM