Calling method by name.

Jacob Carlborg doob at me.com
Wed Feb 2 12:00:20 PST 2011


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.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list