开发者

How to identify if Class reference is an interface?

开发者 https://www.devze.com 2023-04-12 03:03 出处:网络
As in Java I want to know if my reference is declared as Interface. function foo(classRef:Class){ if(cla开发者_运维知识库ssRef.isInterface(){

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号