How can I make sure RTTI is available for a class without instantiating it?

前端 未结 2 559
别那么骄傲
别那么骄傲 2020-12-17 00:24

I\'ve recently posted a question in this forum asking for any advice regarding missing RTTI information in a DXE2 executable.

That post was a stripped down version o

相关标签:
2条回答
  • 2020-12-17 01:11

    Add a reference to the class and make sure that the compiler/linker cannot strip it from the executable.

    unit MyUnit;
    
    interface
    
    type
      TMyClass = class(TObject)
      end;
    
    implementation 
    
    procedure ForceReferenceToClass(C: TClass);
    begin
    end;
    
    initialization
      ForceReferenceToClass(TMyClass);
    
    end.
    

    In production code you would want to place ForceReferenceToClass in a base unit so that it could be shared. The initialization section of the unit that declares the class is the most natural place for the calls to ForceReferenceToClass since the unit is then self-contained.

    Regarding your observation that GetType can locate the type, the very act of calling GetType(TMyClass) adds a reference to the type to the program. It's not that the RTTI is present and FindType cannot find it. Rather, the inclusion of GetType(TMyClass) adds the RTTI to the resulting program.

    0 讨论(0)
  • 2020-12-17 01:13

    I used {$STRONGLINKTYPES ON} and worked very well. Put it on main unit.

    0 讨论(0)
提交回复
热议问题