How to check class inheritance in ActionScript 3?

In ActionScript 3, you can find out if an O object is a C class or a class that extends or implements a C class (directly or indirectly) using ...

if (O is C) {
    ...
}

What I want to do is check if the CC class is spreading or implementing the C class (directly or indirectly) without having to instantiate the object.

In Java, you would use ...

if (C.isAssignableFrom (CC)) {
    ...
}

http://java.sun.com/javase/6/docs/api/java/lang/Class.html#isAssignableFrom(java.lang.Class)

What about ActionScript 3?

Thanks!

+5
source share
6 answers

You can directly call describeType () on CC. You do not need to instantiate the object.

var typeXML:XML = describeType(CC);
if(typeXML.factory.extendsClass.(@type=="C").length() > 0)
{
...

, , .

( Amarghosh: [http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#describeType()][1])

+3

, XML-, flash.utils.describeType

+1

, as3commons-reflect package ( , , ), :

ClassUtils.getImplementedInterfaces(CC)
0

Spicelib reflection. ClassInfo.isType.

, ClassInfo

ClassInfo.forClass(A).isType(B);
0

, . , , .

var child:Child = getChild() as Parent;
if(child != null) {
    ...
}
0

describeType() , . , Class.prototype prototype.isPrototypeOf(). , , , ( , ).

( ), , flash.utils.getDefinitionByName(), , - . SWF, - ApplicationDomain.currentDomain.getDefinitionByName() contextLoader.currentDomain.getDefinitionByName().

, String , . , false, , , .

/**
 * Determines whether the childClass is in the inheritance chain of the parentClass. Both classes must be declared
 * within the current ApplicationDomain for this to work.
 * 
 * @param   childClass
 * @param   parentClass
 * @param   mustBeChild
 */
public static function inheritsFrom(childClass:*, parentClass:*, mustBeChild:Boolean = false) {
    var child:Class,
        parent:Class;

    if (childClass is Class) {
        child = childClass;
    } else if (childClass is String){
        child = getDefinitionByName(childClass) as Class;
    }

    if (! child) {
        throw new ArgumentError("childClass must be a valid class name or a Class");
    }

    if (parentClass is Class) {
        parent = parentClass;
    } else if (parentClass is String){
        parent = getDefinitionByName(parentClass) as Class;
    }

    if (! parent) {
        throw new ArgumentError("parentClass must be a valid class name or a Class");
    }

    if (parent.prototype.isPrototypeOf(child.prototype)) {
        return true;
    } else {
        if (mustBeChild) {
            return false;
        } else {
            if (parent.prototype === child.prototype) {
                return true;
            }
        }
    }

    return false;
}
-1

All Articles