[AS3] Reflection in ActionScript 3

by Bernd on June 25th, 2008

Today i’ve used reflection for the first time in one of my Flex projects. After some google research i found the correct hint

All you need is the function getDefinitionByName(className:String) found in the flash.utils package. The following 2 lines creates a Class Object of my SampleClass

var className:String = “com.firstrowria.SampleClass”;

var classReference:Class = Class(getDefinitionByName(className));

Now we are ready to get an instance

var classInstance:Object = new classReference();

But there is more. What if we want to call a function of our newly created class? No Problem, even with parameters!

var functionName = “sampleFunction”;
var functionName2 = “sampleFunctionWithParameter”;

classInstance[event.functionName]();
classInstance[event.functionName2](”this is my string parameter”);

Posted in category: AS3
Tags: , , ,

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS