【问题标题】:How can I get the name of a class from a static method on that class, in haxe?如何从类的静态方法中获取类的名称,在 haxe 中?
【发布时间】:2013-08-13 08:16:46
【问题描述】:

通常我只会使用Type.getClassName(Type.getClass(this),但显然这不起作用,因为没有this。有什么想法吗?

【问题讨论】:

    标签: haxe


    【解决方案1】:

    如果是静态方法,由于Haxe中没有静态成员继承,你已经知道它属于哪个类了。所以我建议对课程进行硬编码。

    或者,您可以使用...宏!

    import haxe.macro.Context;
    import haxe.macro.Expr;
    class ClassNameHelper {
        macro static public function getClassName():ExprOf<String> {
            return { expr: EConst(CString(Context.getLocalClass().toString())), pos: Context.currentPos() }
        }
    }
    
    class Test {
        public static function main() {
            trace(ClassNameHelper.getClassName()); //Test
        }
    }
    

    【讨论】:

    • 哈!是的,我不知道静态方法继承的事情,但我会继续接受这个答案,因为宏。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多