Attributes and RTTI
Go Up to Attributes (RTTI)
Introduces the concept of attributes, their general use case, and some of their basic restrictions in the Delphi language.
Note: Delphi attributes are not supported in C++Builder. For information about RTTI in C++Builder, see Delphi RTTI and C++Builder.
Introduction
Attributes are a language feature in Delphi that allows annotating types and type members with special objects that carry additional information. This information can be queried at run time. Attributes extend the normal Object-Oriented model with Aspect-Oriented elements.
In general, attributes are useful when building general purpose frameworks that analyze structured types such as objects or records at run time and introduce new behavior based on additional information supplied by the annotated attributes.
Attributes and RTTI
Attributes do not modify the behavior of types or members by themselves. The consumer code must specifically query for their existence and take appropriate actions when this is required. To be able to attach an attribute to an entity in the compiled binary, first you need to have RTTI information emitted for that entity. This means that types that explicitly disable RTTI information are not eligible for attribute annotation. For example, in the following code, SomeCustomAttribute will not be emitted to the compiled binary, because the RTTI information is specifically disabled for the TDerivedObject class.
type
{$RTTI EXPLICIT METHODS([]) PROPERTIES([]) FIELDS([])}
TDerivedObject = class(TObject)
[SomeCustomAttribute()]
procedure Shoot;
end;
Topics
- Declaring Custom Attributes (RTTI)
- Annotating Types and Type Members
- Extracting Attributes at Run Time
- Using Virtual Method Interceptors