How to identify if Class reference is an interface?
As in Java I want to know if my reference is declared as Interface.
function foo(classRef:Class){
if(cla开发者_运维知识库ssRef.isInterface(){
//something
}
}
You can use AS3 Commons Reflect to get this information. Your function would then look something like this:
function foo(classRef:Class)
{
var type:Type = Type.forClass(classRef);
if (type.isInterface)
{
//something
}
}
My own exploring. If class is interface, than in description XML in <factory>
node it will never contain <constructor>
and <extendsClass>
. So, this is a function:
private function isInterface(type : *):Boolean {
var description : XML = describeType(type);
return (description.factory[0].descendants("constructor").length() == 0
&& description.factory[0].descendants("extendsClass").length() == 0);
}
Test:
trace(isInterface(IEventDispatcher));
trace(isInterface(Button));
trace(isInterface(int));
trace(isInterface(XML));
trace(isInterface(String));
Output:
[trace] true
[trace] false
[trace] false
[trace] false
[trace] false
精彩评论