[英]Reading metadata in a Haxe macro
I would like to know how to read metadata from a class (and its methods) in a macro. 我想知道如何从宏中读取类(及其方法)中的元数据。
I tried to modify this example . 我试图修改这个例子 。 I added
:
to see if metadata without them is only available in generated code, but nothing.. I have an empty result in all three cases.. Any ideas? 我补充说
:
看看没有它们的元数据是否仅在生成的代码中可用,但没有。我在所有三种情况下都有一个空结果..任何想法?
@:author("Nicolas")
@debug
class MyClass {
@:range(1, 8)
var value:Int;
@broken
@:noCompletion
static function method() { }
}
class Boot {
static public function main() {
test();
}
macro public static function test() {
trace(haxe.rtti.Meta.getType(MyClass)); // { author : ["Nicolas"], debug : null }
trace(haxe.rtti.Meta.getFields(MyClass).value.range); // [1,8]
trace(haxe.rtti.Meta.getStatics(MyClass).method); // { broken: null }
return haxe.macro.Context.makeExpr({}, haxe.macro.Context.currentPos());
}
}
In order to access the types from a macro, you need to use the haxe.macro.*
API rather than accessing haxe.rtti
. 要从宏访问类型,您需要使用
haxe.macro.*
API而不是访问haxe.rtti
。 The following example will trace both debug
and author
, which are the metadata applied to MyClass
: 以下示例将跟踪
debug
和author
,它们是应用于MyClass
的元数据:
class Boot
{
macro public static function test()
{
switch (haxe.macro.Context.getType("MyClass"))
{
case TInst(cl,_):
trace(cl.get().meta.get());
case _:
}
}
}
In order to get class field metadata, you must go through all fields from cl.get().fields.get()
. 要获取类字段元数据,必须遍历
cl.get().fields.get()
所有字段。
See Context.getType()
, ClassType
and MetaAccess
. 请参见
Context.getType()
, ClassType
和MetaAccess
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.