3
votes

I am new to using RTTI and now I'm stuck on an error.

I am trying to call a procedure by name and pass parameter with an array of TValue. The first problem is that GetParameters returns an array of 0 length (instead of 1), and I can't ignore this an try to call the procedure with an empty array.

Here's the code:

procedure TFormGenPopUpMessaggi.ExecMethod(Form : TObject; MethodName:string; const Args: array of TValue);
var
 R : TRttiContext;
 T : TRttiType;
 M : TRttiMethod;
 LParams : TArray<TRttiParameter>;
begin
  T := R.GetType(TFormAcqGestioneRichiesteAcquisto);
  for M in t.GetDeclaredMethods do
    if (m.Parent = t) and (m.Name = MethodName)then
    Begin
      LParams := m.GetParameters;
      showmessage(IntToStr(Length(LParams)));
      M.Invoke(TFormAcqGestioneRichiesteAcquisto.Create(Self), Parametri);
    End;
end;

procedure TFormGenPopUpMessaggi.EseguiMessaggio(Sender : TObject);
  var Procedura, TipoClasse : String;
      Argomenti : String;
      ArrayArgomenti : Array of TValue;
      IdMessaggio, IdElenco : Integer;
      I : Integer;
      C : TRttiContext;
      O : TObject;
begin
    [...]`
    Procedura := QGenerica.FieldByName('NOME_PROCEDURA').AsString;
    [...]
    TipoClasse := 'U' + Copy(QGenerica.FieldByName('NOME_FORM').AsString, 6, 1000) + '.' +         QGenerica.FieldByName('NOME_FORM').AsString;

    O := (C.FindType(TipoClasse) as TRttiInstanceType).MetaClassType.Create;

    if Length(ArrayArgomenti) = 0 then
      ExecMethod(O, Procedura,[])
    else
      ExecMethod(O, Procedura, ArrayArgomenti);

[...]
End;    

And the called procedure is:

procedure TrovaRichiesta(Id : Integer);

declared in public in another class type TFormAcqGestioneRichiesteAcquisto. The code find the procedure name correctly, create the new form class correctly but don't give me any parameter showmessage(IntToStr(Length(LParams))) result is 0.

All the other variables are correctly initialized. Are there any type of keys to define, in order to enable RTTI?

1
It would probably help if you supplied a complete program, an SSCCE. - David Heffernan
trying to create a SSCCE I have found that my code works outside of my original project (created a new one). The GetParameters also return 1, so it is reading correctly the parameters count. It's something inside my project that don't work but don't know what it could be... Have also added {$M+} in form project and in both units involved but nothing change... The options of both project in delphi ide are exactly the same. There is something else to check?? - user3383021
I don't know. It seems clear that we cannot really help until we have an SSCCE. - David Heffernan
What error do you get? Do you get it at run-time or compile time? Since you can't create a SSCCE: can you condense to code even further and still produce the problem? Can you reproduce it when hardcoding the classname? Where is the class declared? - Jeroen Wiert Pluimers

1 Answers

4
votes

To get detailed RTTI turn on {$TYPEINFO} and {$METHODINFO} compiler directives as explained here.