Delphi: Call a function whose name is stored in a string

后端 未结 10 1342
旧时难觅i
旧时难觅i 2020-12-08 05:30

Is it possible to call a function whose name is stored in a string in Delphi?

10条回答
  •  既然无缘
    2020-12-08 05:57

    function ExecuteMethod(AClass : TClass; AMethodName : String; const AArgs: Array of TValue) : TValue;
    var
      RttiContext : TRttiContext;
      RttiMethod  : TRttiMethod;
      RttiType    : TRttiType;
      RttiObject  : TObject;
    begin
      RttiObject := AClass.Create;
      try
        RttiContext := TRttiContext.Create;
        RttiType    := RttiContext.GetType(AClass);
        RttiMethod  := RttiType.GetMethod(AMethodName);
        Result      := RttiMethod.Invoke(RttiObject,AArgs);
      finally
        RttiObject.Free;
      end;
    end;
    

提交回复
热议问题