Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

delphi - TRttiMethod.Invoke function doesn't work in overloaded methods?

I'm creating an instance of a class using the TRttiMethod.Invoke function , but when the constructor or a method is overloaded, the rtti does not call the proper method.

I wrote a sample app to ilustate my problem.

program ProjectFoo;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Rtti,
  System.SysUtils;

type
  TFoo=class
  public
    constructor Create(Value :  Integer);overload;
    constructor Create(const Value :  string);overload;
    function Bar(value : integer) : Integer; overload;
    function Bar(const value : string) : string; overload;
  end;

{ TFoo }

constructor TFoo.Create(Value: Integer);
begin
   Writeln(Value);
end;

function TFoo.Bar(value: integer): Integer;
begin
   Writeln(Value);
   Result:=value;
end;

function TFoo.Bar(const value: string): string;
begin
   Writeln(Value);
   Result:=value;
end;


constructor TFoo.Create(const Value: string);
begin
   Writeln(Value);
end;

var
 c : TRttiContext;
 t : TRttiInstanceType;
 r : TValue;
begin
  try
   c := TRttiContext.Create;
   t := (c.GetType(TFoo) as TRttiInstanceType);
   r := t.GetMethod('Create').Invoke(t.MetaclassType,[444]);//this works 
   //r := t.GetMethod('Create').Invoke(t.MetaclassType,['hello from constructor string']);//this fails : EInvalidCast: Invalid class typecast
   t.GetMethod('Bar').Invoke(r,[1]);// this works
   //t.GetMethod('Bar').Invoke(r,['Hello from bar']); //this fails : EInvalidCast: Invalid class typecast
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
   readln;
end.

This is a RTTI bug? or exist another way to call the overloaded methods of a class using the RTTI?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is nothing wrong with the TRttiMethod.Invoke method, your issue is located in the GetMethod. This function internally call to the TRttiType.GetMethods and retrieves a pointer to the first method which match with the name passed as parameter. So when you are executing this code t.GetMethod('Create') you always are getting a pointer to the same method.

To execute an overloaded version of the constructor or another method you must resolve the method address to execute based in the parameters, and then call the TRttiMethod.Invoke function.

Check this sample function.

function 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;

Now you can call the constructors or methods of your class in one of these ways

   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]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...