我如何在不先导入类型的情况下解析类? [英] How do I resolveClass without importing the type first?
问题描述
请注意,我在针对Neko的Linux上使用Haxe 3.0.1.
考虑以下代码:
var classObject = Type.resolveClass("Foo");
假设我在与此代码相同的程序包中有一个类Foo
,这应该给我一个Class
对象,其中包含有关Foo
的信息.但是,在我的测试中,仅当我先import Foo;
时它才起作用.如果我不导入Foo
,则resolveClass
会返回null
.这对我来说很奇怪.
Assuming that I have a class Foo
in the same package as this code, this should give me a Class
object with info about Foo
in it. However, in my tests it is only working if I import Foo;
first. If I do not import Foo
then resolveClass
returns null
instead. This is strange to me.
我注意到 Haxe API 文档指出:
该类必须已被编译为可访问的,并且如果该类不在同一包中,则完整的点路径"将被编译.必须作为字符串的一部分给出.
The class must have been compiled to be accessible and if the Class is not in the same package, the full "dot path" must be given as part of the string.
但是对于我来说,尚不清楚什么必须已被编译为可访问的".是指在这种情况下或如何将其应用于我的问题.
But it is not clear to me what "must have been compiled to be accessible" means in this context or how it applies to my problem.
那么,我真的需要import
我想解决的每个类吗,还是我错过了其他事情?
So, do I really need to import
every class that I might want to resolve, or am I missing something else?
推荐答案
首先,应为Type.resolveClass
提供类的完全限定名称. IE. pack.subpack.AClass
.
Firstly, you should give Type.resolveClass
the fully qualified name of the class. I.e. pack.subpack.AClass
.
第二,您可以使用--macro include("pack")
. include
函数是 haxe.macro.Compiler
中的方法. --macro
默认情况下会在该类中查找方法.
Secondly, you may use --macro include("pack")
. Which the include
function is a method from haxe.macro.Compiler
. --macro
by default looks into that class for methods.
最后,haxe中的import my.package.*
语句是惰性的.它告诉编译器解析my.package
中的类型,但一开始并没有真正将所有内容导入my.package
中.
Lastly, the import my.package.*
statement in haxe is lazy. It tell the compiler to resolve types in my.package
, but not really import everything in my.package
in the beginning.
这篇关于我如何在不先导入类型的情况下解析类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!