TRttiMethod.Invoke 함수를 사용하여 클래스의 인스턴스를 만들고 있지만 생성자 또는 메서드가 오버로드되면 rtti가 적절한 메서드를 호출하지 않습니다.TRttiMethod.Invoke 함수가 오버로드 된 메서드에서 작동하지 않습니까?
문제를 해결할 수있는 샘플 앱을 작성했습니다.
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.
이것은 RTTI 버그입니까? 또는 RTTI를 사용하여 클래스의 오버로드 된 메소드를 호출하는 다른 방법이 있습니까?