解决方法
请提供更多的细节,你想实现什么.
我所知道的:
>不可能像这样调用一个随机函数.
>对于类和对象函数(MyObject.Function),这可以用RTTI来完成,但这是很多工作.
>如果你只需要调用一个特定类型的函数(比如说,函数(整数,整数):string),这很简单.
对于最后一个,声明一个函数类型,然后获取一个函数指针并将其转换为:
- 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 to object instance
- Result := TMyFuncType(m)(a,b);
- end;
- {...}
- //use it like this
- var MyClass: TMyClass;
- begin
- MyClass := TMyClass.Create;
- MyClass.Call('Func1',3,5);
- MyClass.Call('Func2',6,4);
- MyClass.Destroy;
- end.