1. Technology

RTTI in Delphi - Extended Run Time Type Information

Using And Understanding New RTTI in Delphi >= 2010

By

Delphi provided Runtime Type Information (RTTI) more than a decade ago. RTTI exposes object's data type that is set into memory at run-time.

Prior to Delphi 2010, RTTI was available to "published" sections of a class. For example, the Object Inspector relied on RTTI to get the list of published properties and events to display, for a component.

The Using RTTI in Delphi provides a great overview of the RTTI classes along with a few examples.

In other environments, like .Net, RTTI is known as "reflection".

You can use RTTI classes to dynamically create an instance of a class, or get the type from an existing object and invoke its methods or access its fields and properties.

RTTI in Delphi 2010 (and Newer)

Support for RTTI in Delphi had a facelift (and that some) in Delphi 2010.

With Delphi 2010 you have the option to have RTTI information for almost everything.

Delphi 2010's RTTI is included for all types (classes, core data types and user defined types) covering not only published members but also public, protected and private elements.

Here's a list of tutorials / articles with new RTTI examples and ideas for usage in Delphi (>=) 2010.

Working with RTTI
Runtime Type Information (RTTI) is a programming paradigm in which information about a type can be obtained at run time. If RTTI generation is enabled, the resulting binary includes special metadata that contains information about types (for example, class ancestry, declared fields, annotated attributes).

Delphi 2010 RTTI Contexts: how they work, and a usage note
Delphi 2010 includes extended support for RTTI, also known as run-time type info or reflection. Many design approaches that have previously only been possible with managed languages like C# and Java because of the code annotation and introspection they required should now be possible in the Delphi world.

Locations vs Values: using RTTI to work with value types
Delphi's Rtti unit is designed in substantial part around TValue, a kind of hold-all record that should be capable of containing almost any Delphi value, along with type information for that value.

Virtual method interception
Delphi XE has a new type in Rtti.pas called TVirtualMethodInterceptor. Essentially, it creates a derived metaclass dynamically at runtime that overrides every virtual method in the ancestor, by creating a new virtual method table and populating it with stubs that intercepts calls and arguments.

Delphi 2010 - RTTI & Attributes
I like to compare RTTI to meta data information stored in a database. If I execute the following SQL statement "select * from employee" how does the database know what to return?

Including:

  • Delphi 2010 RTTI - The basics
  • Using Attributes and TCustomAttribute descendants
  • Exploring TRTTIType in depth
  • Introduction to TValue
  • Exploring TRttiMember Descendants in depth (Part I) Properties and Fields
  • Why I call TRttiContext.Create() and TRttiContext.Free()
  • Exploring TRttiMember Descendants in depth (Part II) Methods
  • TValue in Depth
  • INI persistence the RTTI way
  • Xml Serialization - Basic Usage
  • Xml Serialization - Control via Attributes
  • Attributes: Practical Example- Object to Client Dataset
  • Types by Package... Dynamic plug-in systems.

RTTI and Attributes in Delphi 2010
One of the new features that I’m very excited about is support for Attributes in Win32. Here’s a quick run through of creating and then querying custom attributes.

Create Smaller Delphi XE (and Delphi 2010) Executables: Remove RTTI, Pack EXE
How to use Delphi compiler directives: WEAKLINKRTTI and RTTI EXPLICIT along with an executable packer to have 50% smaller EXEs.

Fun with Delphi Rtti – Building a Unit Dependency Tree
You can build a unit dependency tree, wich shows the direct dependency for each unit in your project using the New Rtti.

Still on Delphi 7? Are you aware of Delphi anonymous methods and generic data types available in Delphi >= 2009?

©2014 About.com. All rights reserved.