私はこのブロックを使用しています:
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;
しかし、その後、アクセス違反が発生し続けます...または無効なキャストポインタークラス(またはその他)。