Explorar o código

added createInstance

Nicolas Cannasse %!s(int64=18) %!d(string=hai) anos
pai
achega
f18f9d6b65
Modificáronse 1 ficheiros con 18 adicións e 0 borrados
  1. 18 0
      std/Type.hx

+ 18 - 0
std/Type.hx

@@ -257,6 +257,24 @@ class Type {
 		return e;
 	}
 
+	/**
+		Creates an instance of the given class with the list of constructor arguments.
+	**/
+	public static function createInstance<T>( cl : Class<T>, args : Array<Dynamic> ) : T untyped {
+		#if flash9
+			return cl.__construct__.call(null,args);
+		#else flash
+			var o = { __constructor__ : cl, __proto__ : cl.prototype };
+			cl["apply"](o,args);
+			return o;
+		#else neko
+			return untyped __dollar__call(__dollar__objget(cl,__dollar__hash("new".__s)),cl,args.__a);
+		#else js
+			if( args.length >= 6 ) throw "Too many arguments";
+			return untyped __new__(cl,args[0],args[1],args[2],args[3],args[4],args[5]);
+		#else error
+		#end
+	}
 
 	/**
 		Similar to [Reflect.createInstance] excepts that the constructor is not called.