Question

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;
Was it helpful?

Solution

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;
    ...   

OTHER TIPS

I am glad that you have found a solution. It is the same thing with $TypeInfo directive. Delphi 7 help says:

Note that if a class is forward declared, the first declaration of the class must be declared with the $M switch.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top