Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Let's say i acquire the name of a class that i made as a String. How can i Instantiate the class with the name contained in that string? I I know it will be derived from a certain parent class, but the actual class will vary.

like image 283
RCIX Avatar asked Sep 08 '10 09:09

RCIX


1 Answers

var instance : MyClass = Type.createInstance(Type.resolveClass("path.to.MyClass"), []);

Few notes:

  • resolveClass() takes the full path (packages included) of the classe you need
  • createInstance() takes as the second argument an array of values that are applied to the constructor. Those values must be in the exact number and must be passed even if they are optional (nulls are good in that case).
like image 150
Franco Ponticelli Avatar answered Oct 03 '22 03:10

Franco Ponticelli