[英]AS3 - Can I know if a class implements an interface (or is a subclass of another class)?
With this code 有了这段代码
function someFunction(classParam:Class):Boolean
{
// how to know if classParam implements some interface?
}
ie Comparing classParam
with IEventDispatcher
interface: classParam
与IEventDispatcher
接口进行比较:
someFunction(EventDispatcher) // returns true
someFunction(Object) // returns false
I know it can't be done with is
operator. 我知道运营商无法做到这
is
。 But, is there a way to do it? 但是,有没有办法做到这一点? Is there a way to know if a class implements some interface?
有没有办法知道一个类是否实现了一些接口? (or is a subclass of another class?)
(或者是另一个类的子类?)
Possible solutions: 可能的解决方案:
A. Creating an object of classParam
and using that object to compare using is
operator. A.创建
classParam
的对象并使用该对象比较using is
运算符。
function someFunction(classParam:Class):Boolean
{
return (new classParam()) is IEventDispatcher
}
B. Using describeType()
B.使用
describeType()
function someFunction(classParam:Class):Boolean
{
var xml:XML = describeType(classParam)
// found "implementsInterface" value in xml and compare to IEventDispatcher
}
There is a way that DOES NOT USE describeType
or creates a new
operator? 有一种方法不使用
describeType
或创建一个new
运算符?
I don't see any way to achieve what you're trying to do except by using describeType
. 除了使用
describeType
之外,我没有看到任何方法来实现你想要做的事情。
It has been created for this purpose, why don't you want to use it? 它是为此目的而创建的,您为什么不想使用它?
Edit: 编辑:
It actually only takes 2 lines to do this : 它实际上只需要2行:
var classDescription:XML = describeType(classParam);
return (classDescription.factory.implementsInterface.(@type == getQualifiedClassName(IEventDispatcher)).length() != 0);
...or just in one, if it's what bothers you: ...或只是在一个,如果它困扰你:
return (describeType(classParam).factory.implementsInterface.(@type == getQualifiedClassName(IEventDispatcher)).length() != 0);
也许本文中的代码示例将提供答案: ActionScript 3.0中的抽象类和方法的运行时检查
Adding to 'Zed-K' response. 添加'Zed-K'响应。 I ended up not needing the .factory part.
我最终不需要.factory部分了。 Here is a example of a test that checks that the class is utilizing and interface.
以下是检查类是否正在使用和接口的测试示例。 '_instance' is the class under test.
'_instance'是被测试的类。
[Test]
public function testInstanceShouldBeIUser():void
{
var classDescription:XML = describeType( _instance );
var type:String = getQualifiedClassName(IUser);
var xmlList:XMLList = classDescription.implementsInterface.(@type == type);
assertTrue("expected IUser", xmlList.length() != 0 );
}
Probably not. 可能不是。 Btw, there is also
describeTypeJSON
, witch is about 5x faster than describeType
. 顺便说一句,还有
describeTypeJSON
,比describeType
快约5倍。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.