ActionScript 3 : Get a Class Reference by Class Name

If you need to get a reference to a class in ActionScript 3, but only know the class name, then you can use the flash.utils.getDefinitionByName to create an instance of the class.

For example:

[code]package
{
import flash.display.Sprite;
import flash.utils.getDefinitionByName;

public class DynamicCall extends Sprite
{
public function DynamicCall()
{
var ClassReference:Class = getDefinitionByName(“String”) as Class;
var s:String = (new ClassReference(“foo=”) as String);
trace(s);
}
}
}[/code]

This basically creates an instance of the String class, from the class name “String”. getDefinitionByName takes the entire class path, so if you wanted to create an instance of MovieClip, you would provide the entire path:

[code] var ClassReference:Class = getDefinitionByName(“flash.display.MovieClip”) as Class;[/code]

Anyways, pretty simple stuff, but can come in useful.

12 Responses to ActionScript 3 : Get a Class Reference by Class Name

  1. Mike J says:

    Cool!Would this be akin to using the following in AS2?var myClass:Test = new _global[“Test”](parm);I’d like to see the reverse become possible too, so that any class you create transparently has a className property available, which returns a string. 🙂

  2. senocular says:

    Mike: You can use flash.utils.getQualifiedClassName() to go backwards and there’s also a flash.utils.describeType() which will tell you a lot more about a class in the form of XML.

  3. Bazard says:

    Hey thanks Senocular.I always wanted to do this in my development but as I still code in AS2, I could not make it so easily.Now I have a good reason to have a closer look to AS3.

  4. Jacob says:

    ActionScript 3, hip hip hooray (not). I almost don’t even have the words Mike. Adobe has let down it’s loyal Mac using fans again with no Flex Builder 2 for us. Over half a year ago I was so excited when I noted a snippet about Flex Builder 2 for OS X, here we are at release time and nothing, nadda. I honestly don’t know what to say. I’m crushed, frustrated, angry, and so forth. Maybe it sounds silly, but it’s true. I’m ready to say forget Apollo too. I’m losing all interest in these corporate jerks. Sorry to bother Mike, but you’re the only one at Adobe who I thought might listen. Adobe has etched itself into a dark, dark corner of my own mindshare, and I’m probably not alone. Sigh. It might be time to turn the tables on Adobe. Thanks for listening Mike. -jt

  5. Arul Prasad says:

    hey Mike,Am not sure about the class, but I’ve seen a very similar method somewhere in the mx.utils package. I dont have Flash 8 installed right now, so cant point the exact location.My point is, if am not wrong, a AS2 implementation of the same was there somewhere hidden in the mx.utils package..

  6. Zeng says:

    I find if this class creat by myself ,it can’t creact. For example: var ClassReference:Class = getDefinitionByName(“com.freelystone.face.SysManage”) as Class;The “com.freelystone.face.SysManage” is a class created by myself.If is run the system print “ReferenceError: Error #1065: “; Can you tell me how to amend my code.

  7. how do you get all fields and methods in the class by reflection? says:

    How do you get references to all fields and methods in the class by reflection? Can do it in Java pretty easily – is it possible with Actionscript?Thanks,Vicki

  8. James says:

    Has anyone managed to instantiate a class from a loaded child swf?eg.mc in the child swf with dynamically generated class definition of ‘MyClass’then in the parent once the child has loadedgetDefinitionByName(“MyClass”); // doesn’t work…Any ideas?Thanks,James

  9. Anon says:

    Like Zeng, I have been trying to implement this type of system using a class I have created and I’m not successful. Is there any trick to get the compiler to resolve your own “com.xyz” location?Thanks!

  10. Unluckily, you have to declare a variable of “myclass” type in order to use getDefinitionByName with that classes.I’ve written a tutorial in As3Guru:http://www.as3guru.com/?p=5

  11. Matthias Eichstedt says:

    @Zeng, Anon, Enrico,actually, you can also prevent the compiler from excluding assumably unused classes by including those classes manually. setting additional compiler argument:-includes=com.test.Test, com.test.AnotherTestwould do the trick.Of course it can be quite annoying to specify 20 or more classes this way; in that case, use the ‘include-libraries’ directive to include a whole swc-lib 🙂

  12. Trevor says:

    Is it possible to use this method for attaching library assets from a loaded child swf? Similar to what Zeng was asking. I want to load a swf with items in the library and then attach those items to the stage from somewhere else. i keep getting told the variable i’m passing to getDefinitionByName is not ddefined…this my function:public static function attachSprite(target:Sprite, linkage:String):Sprite{var ClassDefinition:Class = Class(getDefinitionByName(linkage));var skinObject:Object = new ClassDefinition();var sprite:DisplayObject = target.addChild(DisplayObject(skinObject));return Sprite(sprite);}