how to test object type compatibility?

What is the easiest / best-performing way to tell if a given class is a (direct or indirect) subclass of another?

That might be two separate questions, I guess. But so far, the only way I’ve found to do it is to give all my objects class names or “typename” public member strings (that I assign manually) that contain that of their ancestors, and then test the string (either object.GetType().name or object.typename) using String.IndexOf() to see if it contains the ancestor’s name. Hacky and sad, I’m sure. There must be a better way?


class CommonBase {..}
class A extends CommonBase {..}
class A1 extends A {..}
class A1x extends A1 {..}

class B extends CommonBase {..}
class B1 extends B {..}
..
function DoSomething(anObject : CommonBase)  {
    if (anObject.DescendsFrom(A) {
        doOneThing;
    }
    else if (anObject.DescendsFrom(B) {
        doSomethingElse;
    }
}

That “DescendsFrom()” piece is what I’m trying to find.

See this post on the forums for how it’s done in both C# and JavaScript.

Ah, thank you!

So, something like this would work:

   var a : A = anObject as A;
   if (a) {
        .. do stuff with a ..
   }