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

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -