haxe - Casting Dynamic to an other class -


i know if that's possible cast dynamic other class (partially or totally)

for example, code breaks :

class test {     public function new() {}     public var id: string; }  class main {     public static function main() {         var x:dynamic = jsonparser.parse("{\"id\":\"sdfkjsdflk\"}");         var t:test = cast(x, test);     } } 

with following message

class cast error

however, "test" class has "id" field dynamic object. (that's example, use case more complexe ^^)

so, don't understand how object dynamic one.

this isn't casting dynamic class instance may accomplish same thing:

  • create empty instance of class type.createemptyinstance
  • set of fields dynamic object on new class instance using reflect

example:

import haxe.json;   class test {     public function new() {}     public var id: string; }  class main {     public static function main() {         var x:dynamic = json.parse("{\"id\":\"sdfkjsdflk\"}");         var t:test = type.createemptyinstance(test);         (field in type.getinstancefields(test))             if (reflect.hasfield(x, field))                 reflect.setproperty(t, field, reflect.getproperty(x, field));          trace(t.id);     } } 

Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -