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;
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;
    ...   
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With