Obtaining the RTTI Context

From RAD Studio
Jump to: navigation, search

Go Up to Working with RTTI

RTTI information is stored in the compiled binary, which means it does not need actual initialization. Still, working with this raw information can be quite daunting; hence, the System.Rtti unit is provided in the Delphi RTL. System.Rtti builds up a clean, object-oriented API that can be easily used in any application. To make the developer experience even more pleasant, the Rtti unit provides a special record, called TRttiContext, which manages the lifetime of all objects created during an RTTI work session.

TRttiContext can be considered the cornerstone of RTTI API; all type information queries are carried through this record. TRttiContext is also responsible for the lifetime management of all objects created through it.

To obtain a TRttiContext, you must use the static method TRttiContext.Create:

var
    LContext: TRttiContext;

begin
    { Obtain the RTTI context }
    LContext := TRttiContext.Create();

    // ...

    LContext.Free;
end.
#include <Rtti.hpp>
// ...

int _tmain(int argc, _TCHAR* argv[]) {
    // Obtain the RTTI context
    TRttiContext context;

    // ...
    
    return 0;
}

Note that calls to TRttiContext.Create and TRttiContext.Free are not mandatory for the normal functioning of TRttiContext instances. It is still recommended that you call Create to initialize the RTTI context and Free to release it immediately.

See Also