[haXe] Building classes dynamically

Michael Pliskin pl at tepkom.ru
Fri Apr 13 10:30:09 CEST 2007


Hello Gerry,

  Looks like you're looking for a way to create a class instance by
  name - then look into Type class:
  var class = Type.resolveClass("className");
  var instance = Type.createInstance(class, [constructor_param1, constructor_param2]);

GF> I am not even sure this is possible however it would same me piles of 
GF> lines of code.
GF> I am trying to write a class which will parse xml and dynamically build
GF> the components based on xml content.

GF> It all works using case statements, however I would like to make it more
GF> robust.
GF> I have tried using Reflect however its seems like I just don't get it.

GF> This is what I am trying to do

GF> import someclasses

GF> function createInstance( fp_instanceName:String){

GF> // I do not know how to use the string passed in the argument to create
GF> an instance of the class
GF> var obj = new fp_instanceName();

GF> // now I can access the fields to the class...
GF> Reflect.hasField('SomeProperty');

GF> }

GF> Thanks for any help, once I get this working I will be posting the code
GF> Its a complete set of dynamic UI components.





-- 
Best regards,
 Michael                            mailto:pl at tepkom.ru




More information about the Haxe mailing list