Delphi: Call a function whose name is stored in a string -
is possible call function name stored in string in delphi?
please give more details on trying achieve.
as far know:
- it not possible call random function that.
- for class , object functions (myobject.function) can done rtti, it's lot of work.
- if need call 1 particular type of functions (say, function(integer, integer): string), it's lot easier.
for last one, declare function type, function pointer , cast this:
type tmyfunctype = function(a: integer; b: integer): string of object; tmyclass = class published function func1(a: integer; b: integer): string; function func2(a: integer; b: integer): string; function func3(a: integer; b: integer): string; public function call(methodname: string; a, b: integer): string; end; function tmyclass.call(methodname: string; a, b: integer): string; var m: tmethod; begin m.code := self.methodaddress(methodname); //find method code m.data := pointer(self); //store pointer object instance result := tmyfunctype(m)(a, b); end; {...} //use var myclass: tmyclass; begin myclass := tmyclass.create; myclass.call('func1', 3, 5); myclass.call('func2', 6, 4); myclass.destroy; end.
Comments
Post a Comment