Calling method by name.

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


On 2011-02-02 22:51, Stanislav Blinov wrote:
> On 02/02/2011 11:11 PM, 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.
>>
>
> AFAIK, D2's TypeInfo and friends do have an interface for runtime
> reflection (methods offTi() and getMembers()), though a quick glance
> shows they're not implemented, i.e. return null all the time.

I think the const is incorrect as well on getMembers.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list