1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| import flash.display.Loader;
import flash.errors.IllegalOperationError;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
class ClassLoader extends EventDispatcher {
public static var CLASS_LOADED:String = "classLoaded";
public static var LOAD_ERROR:String = "loadError";
private var loader:Loader;
private var swfLib:String;
private var request:URLRequest;
private var loadedClass:Class;
public function ClassLoader() {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
}
public function load(lib:String):void {
swfLib = lib;
request = new URLRequest(swfLib);
var context:LoaderContext = new LoaderContext();
context.applicationDomain=ApplicationDomain.currentDomain;
loader.load(request,context);
}
public function getClass(className:String):Class {
try {
return loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
} catch (e:Error) {
throw new IllegalOperationError(className + " definition not found in " + swfLib);
}
return null;
}
private function completeHandler(e:Event):void {
dispatchEvent(new Event(ClassLoader.CLASS_LOADED));
}
private function ioErrorHandler(e:Event):void {
dispatchEvent(new Event(ClassLoader.LOAD_ERROR));
}
private function securityErrorHandler(e:Event):void {
dispatchEvent(new Event(ClassLoader.LOAD_ERROR));
}
} |
Partager