RTL.AttributesAndRTTI Sample

From Appmethod 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

This application consists of two projects: rtti_browser and attribute_listing.

Attribute_listing is a console application that displays the attributes used to annotate the classes, fields, procedures, and parameters from the project.

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 | Appmethod 1.16 | Samples and open AttributesAndRTTIDemos.groupproj.
  2. Right-click on either attribute_listing or rtti_browser and choose Activate.
  3. 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.

attribute_listing.dpr

The methods for browsing all the types in the RTTI context and displaying the attributes used to annotate them.

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

Personal tools