2014-03-31 3 views
5

매크로에서 클래스 (및 메서드)에서 메타 데이터를 읽는 방법을 알고 싶습니다. Haxe - 매크로에서 메타 데이터 읽기

나는이 예제를 수정하려고 : https://github.com/HaxeFoundation/HaxeManual/blob/master/md/manual/lf-metadata.md 나는 추가 ""그들없이 메타 데이터가 생성 된 코드 만 기한 알아보기 위해,하지만 아무것도 나는 세 가지 경우에 빈 결과 ..이 없다 ..

어떤 아이디어라도 제발? 매크로에서 유형에 액세스하기 위해

@: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()); 
    } 
} 

답변

6

, 당신은 오히려 haxe.rtti 접근보다 haxe.macro.* API를 사용해야합니다. 다음 예는 MyClass에 적용되는 메타 데이터를 둘 debugauthor, 추적합니다

class Boot 
{ 
    macro public static function test() 
    { 
    switch (haxe.macro.Context.getType("MyClass")) 
    { 
     case TInst(cl,_): 
     trace(cl.get().meta.get()); 
     case _: 
    } 
    } 
} 

클래스 필드 메타 데이터를 얻기 위해, 당신은 cl.get().fields.get()에서 모든 필드를 통과해야합니다.

내가하는 ClassType가 나를 도울 수 있다고보고 있지만, 그걸 얻기 위해 어디 몰랐 Context.getType, ClassType

+0

MetaAccess 당신을 감사하십시오. 고마워요! – Peekmo

+0

꽤 새로운데,'cl'과'_'가 어디에서 유래했는지 자세히 설명해 주시겠습니까? (예 :'case TInst (cl, _) :'및'case _ :') – bigp

+0

@bigp는 패턴 일치 문서 (http://haxe.org/manual/lf-pattern-matching)를 살펴 봅니다. HTML) – Waneck

관련 문제