Browse Source

[rttiUtils] new function callmethod

Exilon 5 years ago
parent
commit
d7560ca83e
1 changed files with 22 additions and 1 deletions
  1. 22 1
      Quick.RTTI.Utils.pas

+ 22 - 1
Quick.RTTI.Utils.pas

@@ -7,7 +7,7 @@
   Author      : Kike Pérez
   Version     : 1.4
   Created     : 09/03/2018
-  Modified    : 03/04/2020
+  Modified    : 14/07/2020
 
   This file is part of QuickLib: https://github.com/exilon/QuickLib
 
@@ -76,6 +76,7 @@ type
     class function FindClass(const aClassName: string): TClass;
     class function CreateInstance<T>: T; overload;
     class function CreateInstance(aBaseClass : TClass): TObject; overload;
+    class function CallMethod(aObject : TObject; const aMethodName : string; aParams : array of TValue) : TValue;
     {$ENDIF}
   end;
 
@@ -123,6 +124,7 @@ var
   rmethod: TRttiMethod;
   rinstype: TRttiInstanceType;
 begin
+  Result := nil;
   rtype := fCtx.GetType(aBaseClass);
   for rmethod in rtype.GetMethods do
   begin
@@ -136,6 +138,25 @@ begin
   end;
 end;
 
+class function TRTTI.CallMethod(aObject : TObject; const aMethodName : string; aParams : array of TValue) : TValue;
+var
+  rtype : TRttiType;
+  rmethod : TRttiMethod;
+  rinstype: TRttiInstanceType;
+  value : TValue;
+begin
+  rtype := fCtx.GetType(aObject.ClassInfo);
+  for rmethod in rtype.GetMethods do
+  begin
+    if CompareText(rmethod.Name,aMethodName) = 0 then
+    begin
+      rinstype := rtype.AsInstance;
+      value := rmethod.Invoke(rinstype.MetaclassType,aParams);
+    end;
+
+  end;
+end;
+
 class destructor TRTTI.Destroy;
 begin
   fCtx.Free;