Mady
Mady

Reputation: 5296

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(classRef.isInterface(){
  //something
}

}

Upvotes: 5

Views: 71

Answers (2)

Gerhard Schlager
Gerhard Schlager

Reputation: 3145

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
    }
}

Upvotes: 4

Timofei Davydik
Timofei Davydik

Reputation: 7304

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

Upvotes: 3

Related Questions