Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi RTTI trouble: GetPropInfo returns nil with {$METHODINFO ON}?

Tags:

delphi

rtti

Is there any possibility that GetPropInfo returns nil even if the given class is declared with correct {$METHODINFO} directives.

  type 
  ... 
  ...
    {$METHODINFO ON}
    TMyClass = class
    private
      fField: integer;
    published
      property Field: integer read fField write fField;
    end;
    {$METHODINFO OFF}
  ...
  ...
  procedure TestRTTI;
  begin
    assert(assigned(GetPropInfo(TMyClass, 'Field')), 'WTF! No RTTI found!');
  end;
like image 725
utku_karatas Avatar asked Dec 03 '08 23:12

utku_karatas


1 Answers

Gotcha! It seems the problem is hidden at the forward declaration that I overlooked. Didn't know that sneaky feature.

It seems the compiler considers only the first declaration of the class to generate RTTI or not so if you have a forward declaration like this...

  type 
    TMyClass = class;   
    ...    
    ...
    {$METHODINFO ON}
    TMyClass = class
    private
      fField: integer;
    published
      property Field: integer read fField write fField;
    end;
    {$METHODINFO OFF}   
    ...   
    ...   
    procedure TestRTTI;   
    begin
      assert(assigned(GetPropInfo(TMyClass, 'Field')), 'WTF! No RTTI found!');   
    end;

... You will get the assertion error. So, for getting the RTTI right, one needs to turn the {$METHODINFO} directive on for the forward declaration, as seen here....

  type 
    {$METHODINFO ON}
    TMyClass = class;   
    {$METHODINFO OFF}   
    ...    
    ...
    TMyClass = class
    private
      fField: integer;
    published
      property Field: integer read fField write fField;
    end;
    ...   
like image 99
utku_karatas Avatar answered Sep 30 '22 15:09

utku_karatas