Skip to content

Commit

Permalink
[rttiUtils] new function callmethod
Browse files Browse the repository at this point in the history
  • Loading branch information
exilon committed Jul 14, 2020
1 parent 242d1a3 commit d7560ca
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion Quick.RTTI.Utils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,6 +76,7 @@ TRTTI = class
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;

Expand Down Expand Up @@ -123,6 +124,7 @@ class function TRTTI.CreateInstance(aBaseClass : TClass): TObject;
rmethod: TRttiMethod;
rinstype: TRttiInstanceType;
begin
Result := nil;
rtype := fCtx.GetType(aBaseClass);
for rmethod in rtype.GetMethods do
begin
Expand All @@ -136,6 +138,25 @@ class function TRTTI.CreateInstance(aBaseClass : TClass): TObject;
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;
Expand Down

0 comments on commit d7560ca

Please sign in to comment.