Create an instance of a class from a string name in Haxe

Say I acquired a class name that I made as a String . How can I create an instance of a class with the name contained in this line? I know that it will be derived from a specific parent class, but the actual class will change.

+8
instantiation dynamic-typing haxe
source share
1 answer
 var instance : MyClass = Type.createInstance(Type.resolveClass("path.to.MyClass"), []); 

A few notes:

  • resolveClass() takes the full path (including packages) of the required class.
  • createInstance() takes an array of values โ€‹โ€‹applied to the constructor as the second argument. These values โ€‹โ€‹must be in the exact number and must be passed, even if they are optional (zeros are good in this case).
+16
source share

All Articles