Tôi đang tạo một thể hiện của một lớp bằng cách sử dụng hàm TRttiMethod.Invoke, nhưng khi hàm tạo hoặc phương thức bị quá tải, thì rtti không gọi phương thức thích hợp.Chức năng TRttiMethod.Invoke không hoạt động trong các phương thức quá tải?
Tôi đã viết một ứng dụng mẫu để khắc phục sự cố của mình.
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.
Đây là lỗi RTTI? hoặc tồn tại một cách khác để gọi các phương thức quá tải của một lớp bằng RTTI?