Calling method by name.

Jacob Carlborg doob at me.com
Thu Feb 3 01:01:35 PST 2011


On 2011-02-02 21:11, Robert Clipsham wrote:
> On 02/02/11 20:00, Jacob Carlborg wrote:
>> On 2011-02-02 20:42, Robert Clipsham wrote:
>>> On 02/02/11 17:55, %u wrote:
>>>> I know is possible to create an object from its name. It's possible to
>>>> call a method from that object if the name is only known at runtime?
>>>>
>>>> Would something like the following be possible?
>>>>
>>>> string classname, methodname;
>>>> // Ask the user for class and method.
>>>> auto obj = Object.factory(classname);
>>>> invoke(methodname, obj, param1, param2);
>>>>
>>>> Thanks
>>>
>>> As far as I'm aware there is no way to do this in any of the standard
>>> libraries. This said, it is possible (at least to some extent), to do
>>> some runtime reflection by parsing symbols out of the object file and
>>> demangling them - this is what I'm using, but it's not as extensive as
>>> what you're requesting, it's very use specific (mainly missing calling
>>> methods with any number of parameters). It should be possible with some
>>> work though.
>>
>> Another possibility would be to using __traits with some allMembers,
>> collecting all the names and a delegate in a hash and store it in the
>> object.
>
> If it's possible to do this, you should, it's what I'd be using if I had
> access to it, unfortunately, there's no such love for D1.

Same here, but you would probably need to inherit from a common base 
class or use a mixin.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list