Delphi: invoke constructor raises EInvalidCast

橙三吉。 提交于 2021-01-27 06:53:54

问题


I'm trying to invoke a constructor obtained via RTTI (running D2010 version 14.0.3593.25826). The constructor takes a mixture of strings and objects as its arguments, all of which should be initialized to '' or nil. (Disclaimer: I know that the desired constructor will be the one with maximum number of parameters, hence the weird-looking, although suboptimal design.)

The code goes as follows:

program sb_rtti;
{$APPTYPE CONSOLE}
uses RTTI, TypInfo, SysUtils;

type

TMyClass = class (TObject)
  FField1:  string;
  FObject1: TObject;
public
  constructor Create(Field1: string = ''; Object1: TObject = nil);
end;

constructor TMyClass.Create(Field1: string; Object1: TObject);
begin
  FField1 := Field1;
  FObject1 := Object1;
end;

function GetConstructor(rType: TRttiType) : TRttiMethod;
var
  MaxParams:  integer;
  Methods:    TArray<TRttiMethod>;
  Method:     TRttiMethod;
  Params:     TArray<TRttiParameter>;
begin
  Methods := rType.GetMethods('Create');
  MaxParams := 0;
  for Method in Methods do begin
    Params := Method.GetParameters();
    if (Length(Params) > MaxParams) then begin
      Result := Method;
      MaxParams := Length(Params);
    end;
  end;
end;

procedure InitializeParam(Param: TRttiParameter; ActualParam: TValue);
begin
  if (Param.ParamType.TypeKind = TTypeKind.tkClass) then begin
    ActualParam := TValue.From<TObject>(nil);
  end else if (Param.ParamType.TypeKind = TTypeKind.tkString) then begin
    ActualParam := TValue.From<string>('');
  end else if (Param.ParamType.TypeKind = TTypeKind.tkUString) then begin
    ActualParam := TValue.From<UnicodeString>('');
  end else begin
    // Other types goes here
  end;
end;

var
  Context:      TRttiContext;
  Constr:       TRttiMethod;
  Params:       TArray<TRttiParameter>;
  ResultValue:  TValue;
  rType:        TRttiType;
  ActualParams: array of TValue;
  i:            integer;
  CurrentParam: TRttiParameter;
begin
  Context := TRttiContext.Create();
  rType := Context.GetType(TypeInfo(TMyClass));
  Constr := GetConstructor(rType);
  try
    if (Constr <> nil) then begin
      Params := Constr.GetParameters();
      SetLength(ActualParams, Length(Params));
      for i := 0 to Length(Params) - 1 do begin
        CurrentParam := Params[i] as TRttiParameter;
        InitializeParam(CurrentParam, ActualParams[i]);
      end;
      ResultValue := Constr.Invoke(rType.AsInstance.MetaclassType, ActualParams);
    end;
  except
    on E : Exception do
      WriteLn(E.ToString);
  end;
  ReadLn;
end.

Now, when the line ResultValue := Constr.Invoke(rType.AsInstance.MetaclassType, ActualParams); is executed, an EInvalidCast exception is raised. The exception may be traced to the TValue.Cast-method at line 1336.

However, the meat of the problem seems to be found at the previous point in the call stack, more precisely at line 4093 in rtti.pas (argList[currArg] := Args[i].Cast(parList[i].ParamType.Handle);).

My bet is that I'm using rtti in ways I'm not supposed to, yet, I can't find the "right way" described anywhere. Can anybody please point me in the right direction? Thanks!


回答1:


You have a problem in the InitializeParam procedure because in the assignment of the ActualParam parameter, you are setting the value of the local copy of that parameter – remember that TValue (the type of ActualParam) is a record. So to fix the problem you must pass the ActualParam as a var parameter.

procedure InitializeParam(Param: TRttiParameter; var ActualParam: TValue);



回答2:


It just occurred to me to hard-code the argument initialization by replacing

for i := 0 to Length(Params) - 1 do begin
  CurrentParam := Params[i] as TRttiParameter;
  InitializeParam(CurrentParam, ActualParams[i]);
end;

with

ActualParams[0] := TValue.From<string>('');
ActualParams[1] := TValue.From<TObject>(nil);

which solves the problem.



来源:https://stackoverflow.com/questions/7846824/delphi-invoke-constructor-raises-einvalidcast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!