Delphi 尝试调用函数tru RTTI;无效类型转换

Delphi 尝试调用函数tru RTTI;无效类型转换,delphi,rtti,delphi-10.2-tokyo,Delphi,Rtti,Delphi 10.2 Tokyo,我试图在运行时从类对象调用函数 找到对象,找到方法,并将参数存储在TValue数组中 调用TRttiMetho.Invoke时,会触发以下错误 无效类型转换 怎么了 您使用错误的第一个参数调用Invoke。如果该方法是常规方法,则需要传递调用该方法的对象的实例。如果是类方法,则需要传递该方法的TClass 他甚至解释了这一点 您正在传递vitype,它是TRttiInstanceType类型的实例。您使用错误的第一个参数调用Invoke。如果该方法是常规方法,则需要传递调用该方法的对象的实例。

我试图在运行时从类对象调用函数

找到对象,找到方法,并将参数存储在TValue数组中

调用TRttiMetho.Invoke时,会触发以下错误

无效类型转换

怎么了


您使用错误的第一个参数调用Invoke。如果该方法是常规方法,则需要传递调用该方法的对象的实例。如果是类方法,则需要传递该方法的TClass

他甚至解释了这一点


您正在传递vitype,它是TRttiInstanceType类型的实例。

您使用错误的第一个参数调用Invoke。如果该方法是常规方法,则需要传递调用该方法的对象的实例。如果是类方法,则需要传递该方法的TClass

他甚至解释了这一点

您正在传递vitype,它是TRttiInstanceType类型的实例

    vcontext: TRTTIContext;
    vtype: TRttiType;
    vitype: TRttiInstanceType;
    vmethod: TRttiMethod;
    vparams: TArray<TRttiParameter>;
    vparam: array of TValue;

    begin
      vcontext := TRttiContext.Create;
      for vtype in vcontext.GetTypes do
      begin
        if (vtype.QualifiedName = 'somemodule.sometype') then // this is found
        begin
          if vtype.IsInstance then
          begin
            vitype := (vtype as TRttiInstanceType);
            for vmethod in vitype.GetMethods do
            begin
             if (vmethod.Name = 'Somefunction') then  // this is found
             begin
               vparams := vmethod.GetParameters;  // actually I know tha there are 2 Parameters
               SetLength(vparam, Length(vparams));
               vparam[0] := TValue.From(Ord(SomeEnum));
               vparam[1] := TValue.From<TSomeObject>(Object);
               vmethod.Invoke(vitype, vparam);    // Invalid Typecast here in
             end;
           end;
         end;
       end;
     end;
     vcontext.Free;
   end;
    if (cls <> nil) and not cls.InheritsFrom(TRttiInstanceType(Parent).MetaclassType) then
      raise EInvalidCast.CreateRes(@SInvalidCast);