得到的场的类型HAXE(反射API)

问题描述 投票:8回答:1

我有一个类:

class MyClass {
    private var num : Int;
}

我想知道的是,字段类型Int不管它可以是null例如当前值。

reflection haxe
1个回答
4
投票

您可以在运行时不能做没有编译时的信息。您可与RTTI做到这一点,或宏。 RTTI会更容易实现,虽然这可能是一个慢一点,如果你需要多次解析RTTI。

然后,您的类将成为:

@:rtti
class MyClass {
    private var num : Int;
}

并获得字段类型:

var rtti = haxe.rtti.Rtti.getRtti(MyClass);
for (field in rtti.fields) {
    if (field.name == "num") {
        switch (field.type) {
            case CAbstract(name, _):
                trace(name); // Int
            case _:
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.