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

Is it likely that GetPropInfo returns nil, even if the class is declared with the 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;
+5
source share
2 answers

Gotcha! The problem seems to be hidden in the statement I missed. I did not know this insightful trait.

It seems the compiler only considers the first declaration of the class to generate RTTI or not if you have a direct 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 receive an approval error. So, to obtain RTTI rights, you need to include the {$ METHODINFO} directive for forward declaration, as shown here.

  type 
    {$METHODINFO ON}
    TMyClass = class;   
    {$METHODINFO OFF}   
    ...    
    ...
    TMyClass = class
    private
      fField: integer;
    published
      property Field: integer read fField write fField;
    end;
    ...   
+5
source

, . $TypeInfo. Delphi 7 :

, , $M.

P.S.: $M+/- = $TypeInfo On/Off

+1

All Articles