ソースを参照

Add Create function that accepts arguments

JECL 3 年 前
コミット
d9f3d88c91
1 ファイル変更12 行追加5 行削除
  1. 12 5
      Quick.RTTI.Utils.pas

+ 12 - 5
Quick.RTTI.Utils.pas

@@ -75,6 +75,7 @@ type
     {$IFNDEF FPC}
     class function FindClass(const aClassName: string): TClass;
     class function CreateInstance<T>: T; overload;
+    class function CreateInstance<T>(const Args: array of TValue): T; overload;
     class function CreateInstance(aBaseClass : TClass): TObject; overload;
     class function CallMethod(aObject : TObject; const aMethodName : string; aParams : array of TValue) : TValue;
     {$ENDIF}
@@ -98,7 +99,12 @@ begin
 end;
 
 class function TRTTI.CreateInstance<T>: T;
-var
+begin
+  CreateInstance<T>([]);
+end;
+
+class function TRTTI.CreateInstance<T>(const Args: array of TValue): T;
+var
   value: TValue;
   rtype: TRttiType;
   rmethod: TRttiMethod;
@@ -107,14 +113,15 @@ begin
   rtype := fCtx.GetType(TypeInfo(T));
   for rmethod in rtype.GetMethods do
   begin
-    if (rmethod.IsConstructor) and (Length(rmethod.GetParameters) = 0) then
+
+    if (rmethod.IsConstructor) and (Length(rmethod.GetParameters) = Length(Args) ) then
     begin
       rinstype := rtype.AsInstance;
-      value := rmethod.Invoke(rinstype.MetaclassType,[]);
+      value := rmethod.Invoke(rinstype.MetaclassType,Args);
       Result := value.AsType<T>;
       Exit;
     end;
-  end;
+  end;
 end;
 
 class function TRTTI.CreateInstance(aBaseClass : TClass): TObject;
@@ -137,7 +144,7 @@ begin
     end;
   end;
 end;
-
+
 class function TRTTI.CallMethod(aObject : TObject; const aMethodName : string; aParams : array of TValue) : TValue;
 var
   rtype : TRttiType;