RTL.AttributesAndRTTI Sample
Contents
This sample demonstrates how to use the RTTI API.
Location
You can find the AttributesAndRTTI sample project at:
- Start > Programs > Embarcadero RAD Studio > Samples > Delphi > RTL > AttributesAndRTTI > rtti_browser
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Description
Rtti_browser is a VCL application that displays the hierarchy of the public types available in the current RTTI context. Clicking a class, method, property, or parameter in the tree view displays all their RTTI properties in the list view below.
How to Use the Sample
- Navigate to Start | Programs | Embarcadero RAD Studio Sydney | Samples and open
rtti_browser.dproj
. - Press
F9
or choose Run > Run.
Files
The project has three source files:
File | Contains |
---|---|
DemoAttr.pas |
The implementation of MyDemoAttribute, a user-defined custom attribute, and of TDemoObject, the corresponding annotated class. |
Form_Main.pas |
TMainForm, the class corresponding to the main form. |
Classes
- MyDemoAttribute, derived from TCustomAttribute, provides two constructors that can be used to annotate a class or its members.
- TDemoObject is a demo class with one field and one procedure. The class, the field, the procedure, and the parameter of the procedure are all annotated using MyDemoAttribute.
- TMainForm retrieves all the RTTI information from the context and displays it on the form.
Implementation
- A TRttiContext object is used to get type information for all the public types.
- The ListAttributes method gets all the fields, properties, methods, and parameters and, for each of them, it displays the corresponding attribute values.
- TRttiType.QualifiedName is used to get the qualified name for a class. For the other RTTI objects (methods, parameters, fields, and properties), their qualified name is obtained by concatenating the qualified name of their parent with their own name.
- The hierarchy of types is displayed using a TTreeView component and the properties for each node are displayed using a TListView component.
Uses
- TCustomAttribute
- TRttiContext
- TRttiType.QualifiedName
- TRttiObject.Parent
- TRttiNamedObject.Name
- TTreeView
- TListView