title: Rtti invoke a class method says invalid type cast
body: <p>I have made a unit that's very generic and settings objects in this are all TObjects i don't want to make uses of any units that's why i'm doing this. So my approach is to use RTTI to call everything. But now i face a problem where i can call all the functions and give arguments and everything but when the method is a class procedure/function i can't call it and it says invalid type cast.</p>
  
  <p>I checked on embarcadero's website it says that when we call rtti.invoke on a classmethod we have to set the first argument in Args as a class reference. I tried that but it doesn't work. Take a look at my code:</p>
  
  <pre><code>function TSomething.ExecMethodAndRet(MethodName: string;
    Args: array of TValue): TObjectList&lt;TObject&gt;;
  var
   R : TRttiContext;
   T : TRttiType;
   M : TRttiMethod;
   lArgs : array of TValue;
   i : integer;
  begin
    T := R.GetType(MainObj.ClassInfo);
    for M in t.GetMethods do
      if (m.Parent = t) and (UpperCase(m.Name) = UpperCase(MethodName))then
      begin
        if (m.IsClassMethod) then
        begin
          for I := 0 to Length(Args) do
            lArgs := [args[i]];
          lArgs := [MainObj] + lArgs;
          result := M.Invoke(MainObj, Args).AsType&lt;TObjectList&lt;TObject&gt;&gt;; &lt;- this will say invalid type cast
        end
        else
          result := M.Invoke(MainObj, Args).AsType&lt;TObjectList&lt;TObject&gt;&gt;; &lt;- this one works when it's not a classMethod that's why i made a condition
      end;
  end;
  </code></pre>
  
  <p>I don't know what i'm doing wrong. Maybe it's not possible to do it without knowing the type of the object. My Main Obj is a TObject that is of the required type and i can call the methods of it. But That class procedure is really giving me a hard time.</p>
  
  <p>Someone knows how i could achieve this?</p>