我正在使用这个块:
procedure ExecMethod(Target: TClass; const MethodName: string; const Args: array of TValue);
var
LContext: TRttiContext;
LType: TRttiType;
LMethod: TRttiMethod;
begin
LType := LContext.GetType(Target);
for LMethod in LType.GetMethods do
if (LMethod.Parent = LType) and (LMethod.Name = MethodName) then begin
LMethod.Invoke(Target.Create, Args);
break;
end;
end;
像这样:
ExecMethod(TFuncClass, 'Test1', []);
ExecMethod(TFuncClass, 'Test2', ['hey']);
ExecMethod(TFuncClass, 'Test3', [100]);
在这堂课上:
TFuncClass = class(TObject)
published
procedure Test1;
procedure Test2(const str: string);
procedure Test3(i: integer);
// there's more, each one with different prototype
end;
var
FuncClass: TFuncClass;
但是,我不断收到访问冲突......或无效的强制转换指针类(或其他)......