Idealism is what precedes experience; cynicism is what follows...

Friday, November 16, 2007

Adobe Flex : Dynamically loading classes at runtime

Loading classes dynamically is a powerful and very useful technique.
Using the SWFLoader in the controls package makes doing this straightforward ...

var libraryLoader:SWFLoader = new SWFLoader();
libraryLoader.source = "Library.swf";

if( libraryLoader.loaderContext.applicationDomain.hasDefinition( "Foo" ) )
{
var FooClass:Class = libraryLoader.loaderContext.applicationDomain.getDefinition( "Foo" ) as Class;
var fooInstance:* = new FooClass();
}


An interesting aspect with this technique is the theoretical ability to have several classes with an equal fully qualified name in the same application instance as long as they are loaded from different applicationdomains. I cant foresee what eventually will happen in the case of a casting collision, but I intend to find out as soon as I have 15 minutes to spend.

No comments:

My Network