Overview

Run-Time Type Information (RTTI)

RTTI provides mechanism to inspect and interact with types and their metadata at runtime.

Core Functions

Function Description
TypeInfo(type) Returns TTypeInfo for a given type name.
TypeOf(variable) Returns TTypeInfo for the runtime type of a variable.
RTTIRawAttributes Returns an array of all attribute instances applied in the program.

TTypeInfo Members

Member Description
Name The name of the type.
Kind Type category (e.g. tkInteger, tkClass).
InheritsFrom(other) Checks for inheritance relationship.

Symbol Inspection

The TSymbols class allows for low-level inspection of the program's symbol table.

Method Description
CreateMain Constructor for the root symbol table.
CreateUnit(name) Constructor for a specific unit's symbol table.
First / Next / Last Iteration methods.
Eof True if at the end of the table.
Name / Caption Symbol identification.
SymbolType Category of the current symbol.
GetMembers Returns a symbol table for the members of the current symbol.

Example: Type Inspection

type
  TBase = class end;
  TSub = class(TBase) end;

var c : TClass := TSub;
var info := TypeOf(c);

PrintLn('Type: ' + info.Name);
if info.InheritsFrom(TypeInfo(TBase)) then
   PrintLn('Inherits from TBase');

Accessing Attributes

You can retrieve all attribute instances in a program using RTTIRawAttributes and filter them based on their target.

type
  TTest = class end;

var attrs := RTTIRawAttributes;
for var i := 0 to attrs.Length - 1 do begin
  if attrs[i].T = TypeOf(TTest) then begin
     if attrs[i].A is TCustomAttribute then
        PrintLn('Found attribute');
  end;
end;
On this page