creating anonymous types using reflection

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

creating anonymous types using reflection

François Rey
Is there a way to create anonymous types such as {x:1, y:2} at runtime using
reflection?
I noticed hscript (http://code.google.com/p/hscript/) does not have this
capability, it can't even create classes.

The reason I ask is because it would make it a lot easier I to configure some
tweening library by xml or just plain text.

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: creating anonymous types using reflection

Tony Polinelli

So you want to create an empty anonymous object, and then set properties to it? 

class Object implements Dynamic {}

class Main 
{
static function main()
 {
         var anonymous = Type.createInstance(Object, []); anonymous.test = "val"; Reflect.setField(anonymous, "test2", "val2"); trace(anonymous.test); trace(anonymous.test2);
 }
}




2010/4/7 François Rey <lists.motion-twin.com@francois.rey.name>
Is there a way to create anonymous types such as {x:1, y:2} at runtime using
reflection?
I noticed hscript (http://code.google.com/p/hscript/) does not have this
capability, it can't even create classes.

The reason I ask is because it would make it a lot easier I to configure some
tweening library by xml or just plain text.

--
haXe - an open source web programming language
http://haxe.org



--
Tony Polinelli
http://touchmypixel.com

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: creating anonymous types using reflection

François Rey
On Wednesday 07 April 2010 08:33:29 Tony Polinelli wrote:

> So you want to create an empty anonymous object, and then set properties to
> it?
>
> class Object implements Dynamic {}
>
> class Main
> {
>  static function main()
>  {
>          var anonymous = Type.createInstance(Object, []); anonymous.test =
> "val"; Reflect.setField(anonymous, "test2", "val2"); trace(anonymous.test);
> trace(anonymous.test2);
>  }
> }
>
Wow that's easy, thanks.
So I guess it could easily be added to hscript, right?


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: creating anonymous types using reflection

Tony Polinelli
cant see why not


2010/4/7 François Rey <lists.motion-twin.com@francois.rey.name>
On Wednesday 07 April 2010 08:33:29 Tony Polinelli wrote:
> So you want to create an empty anonymous object, and then set properties to
> it?
>
> class Object implements Dynamic {}
>
> class Main
> {
>  static function main()
>  {
>          var anonymous = Type.createInstance(Object, []); anonymous.test =
> "val"; Reflect.setField(anonymous, "test2", "val2"); trace(anonymous.test);
> trace(anonymous.test2);
>  }
> }
>
Wow that's easy, thanks.
So I guess it could easily be added to hscript, right?


--
haXe - an open source web programming language
http://haxe.org



--
Tony Polinelli
http://touchmypixel.com

--
haXe - an open source web programming language
http://haxe.org