views:

361

answers:

2

If all I have is an instance of an object, can I call a static method of its class? For fun, let's say I don't know what the name of the class is, only the name of the static method.

Can I do this? How do I do this?

+1  A: 

Get the class reference by it's instance.

var className:string = getQualifiedClassName(object); //returns the class name    
var classObj:Class = getDefinitionByName(className) as Class; //get a Class object

Also see this - http://stackoverflow.com/questions/1137730/how-to-get-type-of-variable-and-instantiate-it

Chetan Sastry
Just realized that you probably can't call a static method even with a reference to the Class object. I guess this answer is only halfway there. I don't have access to flex compiler, sorry.
Chetan Sastry
+5  A: 

You can use the Object's constructor property to get a reference to that object's class object, you can then call the static variable from that class object

    package{
        import flash.display.Sprite;

        public class Test extends Sprite{

         public function Test(){
          var variable : A = new A();
          trace((variable as Object).constructor.a);

         }
        }
    }
    class A{
       static const a : String = "test";
    }
SquareRootOf2
+1 for simplicity.
Chetan Sastry
Keep in mind that the constructor property does not work on some Proxy-based classes like XML and XMLList.
Richard Szalay