RTL.AttributesAndRTTI Sample

From RAD Studio Code Examples
Jump to: navigation, search

This sample demonstrates how to use the RTTI API.

Location

You can find the AttributesAndRTTI sample project at:

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

  1. Navigate to Start | Programs | Embarcadero RAD Studio Rio | Samples and open rtti_browser.dproj.
  2. 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

See Also