Jump to content

RTTI does not work correctly with TUniForm


j_kour

Recommended Posts

Hi,

TUniForm does not pass correct information to RTTI. 

Say, you have this declaration:

type
  TMainForm = class(TUniForm)
  private
    { Private declarations }
  public
    procedure sayHi;
  end;

The RTTI code should should reveal all the methods in the class.

  var fCtx: TRttiContext:= TRttiContext.Create;
  var form: TMainForm:=MainForm;
  var rtype: TRttiType:= fCTx.GetType(form.ClassInfo);

  var gg: TArray<string>;
  for var method: TRttiMethod in rtype.GetMethods do
  begin
    SetLength(gg, Length(gg) + 1);
    gg[Length(gg) - 1]:=method.Name;
  end;
  fCtx.Free;

gg array should list all the methods in the class but it misses the sayHi method.

Unfortunately, forcing the compiler to inspect all methods using the following directive does not work

{$RTTI METHODS([vcPublic, vcProtected, vcPrivate])}

On the other hand, if you declare the following class

  TTestClass = class
  private
    procedure method1;
  public
    procedure method2;
  end;

and run the same code gg lists method2 (but, strangely, not method1???).

I suspect the function that returns TMainForm messes up RTTI.

Can anyone think of any work around?

Link to comment
Share on other sites

I changed this

{$RTTI METHODS([vcPublic, vcProtected, vcPrivate])}

to this

{$RTTI EXPLICIT METHODS([vcPublic, vcProtected, vcPrivate])}

and it works now. 

This directive forces RTTI to ignore RTTI information from the ancestor classes and treat each class independently.

The downside is I have to add the directive to every class but it is not such a big deal.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...