That is known that overloaded methods works not so well in Rtti Delphi by default. To fix this RRUZ created this method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
function TRP.RttiMethodInvokeEx(const MethodName:string; RttiType : TRttiType; Instance: TValue; const Args: array of TValue): TValue; var Found : Boolean; LMethod : TRttiMethod; LIndex : Integer; LParams : TArray<TRttiParameter>; begin Result:=nil; LMethod:=nil; Found:=False; for LMethod in RttiType.GetMethods do if SameText(LMethod.Name, MethodName) then begin LParams:=LMethod.GetParameters; if Length(Args)=Length(LParams) then begin Found:=True; for LIndex:=0 to Length(LParams)-1 do if LParams[LIndex].ParamType.Handle<>Args[LIndex].TypeInfo then begin Found:=False; Break; end; end; if Found then Break; end; if (LMethod<>nil) and Found then Result:=LMethod.Invoke(Instance, Args) else raise Exception.CreateFmt('method %s not found',[MethodName]); end; |
So, you can use them like
1 2 3 4 5 |
r := RttiMethodInvokeEx('Create', t, t.MetaclassType, [444]); r := RttiMethodInvokeEx('Create', t, t.MetaclassType, ['hello from constructor string']); r := RttiMethodInvokeEx('Create', t, t.MetaclassType, []); RttiMethodInvokeEx('Bar', t, r.AsObject , ['this is a string']); RttiMethodInvokeEx('Bar', t, r.AsObject , [9999]); |