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
Post a Comment