www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - introspection experiment

reply "Lloyd Dupont" <ld-REMOVE galador.net> writes:
I'd like to explore my object's properties through some kind of 
introspection.
As object.getMembers()  always return null it feels like I will have to do 
it manually.
Thankfully someone pointed out to me that, with mixin and __trait I have 
powerful too to do so...

Anyway, I'm at the stage of handcrafted experiment.

Now I'm trying to (manually so far) write some method which can return any 
property through a name (code below)
but it fails because "Object" is not a catch all which can be int as well .. 
object!

How would you go about fixing, making the following code work?
(it doesn't work because ABC() can't become an 'object delegate()' but how 
would I make a generic method otherwise?)
==================================
class Foo
{
    immutable static members = [__traits(derivedMembers, Foo)];

    private int _abc;
     property public int ABC() { return _abc; }
     property public void ABC(int value) { _abc = value; }

    private Foo _foo;
     property public Foo FOO() { return _foo; }
     property public void FOO(Foo value) { _foo = value; }

    Object delegate()[string] getmembers;

    this()
    {
        getmembers["ABC"] = delegate Object() { return ABC(); };
        getmembers["FOO"] = delegate Object() { return FOO(); };
    }
}
================================== 
Jun 14 2011
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
While my code is ugly as sin, it might be useful to look at the
prepareReflection function in my web.d

http://arsdnet.net/dcode/web.d


It uses the derived members trait to loop through them all, and
create a runtime delegate that converts a string hash into it's
arguments and it's return value into a variety of formats.

End result is you can do:

reflection.objects["Foo"].functions["ABC"].dispatcher(...);

to call the function. It's got some assumptions in there to work
with web urls and returns things wrapped in json values, but the
bulk of it might be useful to you.
Jun 14 2011
parent "Lloyd Dupont" <ld-REMOVE galador.net> writes:
Thanks for that! It'll need some digesting... but it's already giving me 
some idea! :)


"Adam D. Ruppe"  wrote in message news:it7rlo$2ql3$1 digitalmars.com...

While my code is ugly as sin, it might be useful to look at the
prepareReflection function in my web.d

http://arsdnet.net/dcode/web.d

End result is you can do:

reflection.objects["Foo"].functions["ABC"].dispatcher(...);
Jun 15 2011