Technology Programming

Using RTTI in Delphi

Delphi provided Runtime Type Information (RTTI) more than a decade ago. Yet even today many developers aren't fully aware of its risks and benefits. This article provides an overview of the RTTI usage in Delphi for Win32.

In short, Runtime Type Information is information about an object's data type that is set into memory at run-time.
You are probably using this powerful but unfortunately inadequately documented feature of Delphi in your every day projects.

RTTI provides a way to determine if an object's type is that of a particular class or one of its descendants. The is and as operators commonly used in Delphi language, rely on the presence of some form of RTTI. The is operator, which performs dynamic type checking, is used to verify the actual runtime class of an object. The as operator performs checked typecasts.
In Delphi 7 (and 2005) the typinfo unit defines type, classes and objects you use when working with the RTTI.

RTTI in Delphi - Explained

The "Run-Time Type Information In Delphi - Can It Do Anything For You?" article by Brian Long provides a great introduction to the RTTI capabilities of Delphi. Brian explains that the RTTI support in Delphi has been added first and foremost to allow the design-time environment to do its job, but that developers can also take advantage of it to achieve certain code simplifications.
This article also provides a great overview of the RTTI classes along with a few examples. Examples include: Reading and writing arbitrary properties, common properties with no common ancestor, copying properties from one component to another, etc.

RTTI in Delphi - An Example

Here's a small example of the powers of the RTTI. This example shows how to obtain the ancestry of a component using the ClassType and ClassParent properties. It uses a button and a list box on a form. When the user clicks the button, the name of the button’s class and the names of its parent classes are added to the list box (found in the Delphi Help).
~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TForm1.Button1Click(Sender: TObject) ;

var
   ClassRef: TClass;
begin
   ListBox1.Clear;
   ClassRef := Sender.ClassType;
   while ClassRef <> nil do
   begin
     ListBox1.Items.Add(ClassRef.ClassName) ;
     ClassRef := ClassRef.ClassParent;
   end;

end;
~~~~~~~~~~~~~~~~~~~~~~~~~
The list box contains the following strings after the user clicks the button:

TButton
TButtonControl
TWinControl
TControl
TComponent
TPersistent
TObject

RTTI in Delphi - More Examples

Using And Understanding New RTTI in Delphi >= 2010

Support for RTTI in Delphi had a facelift (and that some) in Delphi 2010. Go for Using And Understanding New RTTI in Delphi >= 2010 RTTI is really great. You can use it in many different ways to those provided here. I hope you'll find plenty other ways to exploit this remarkably under-used feature of Delphi.
SHARE
RELATED POSTS on "Technology"
WordPress - How to Set up a New Theme to WordPress 3.
WordPress - How to Set up a New Theme to WordPress 3.
Solution of Creative Web Design
Solution of Creative Web Design
The three disciplines of User Experience
The three disciplines of User Experience
Web Design Sheffield Options For Professional Enterprises
Web Design Sheffield Options For Professional Enterprises
Do you have what it takes?
Do you have what it takes?
Segway Cost
Segway Cost
Microsoft Access 2010: What's Coming with Office 2010?
Microsoft Access 2010: What's Coming with Office 2010?
Companies of Web Development in Ireland Provide Designs that Work
Companies of Web Development in Ireland Provide Designs that Work
Penguin Update to Put Red-Flags on Negative SEO
Penguin Update to Put Red-Flags on Negative SEO
Innovative web 2 design templates can make your business famous quickly
Innovative web 2 design templates can make your business famous quickly
Building A Search Engine Friendly Website
Building A Search Engine Friendly Website
Exceptional Advice To Build Up Your Internet Marketing
Exceptional Advice To Build Up Your Internet Marketing
The Benefits of Selecting The Right Hosting Company
The Benefits of Selecting The Right Hosting Company
Is There a Methodology for Making Successful Logos
Is There a Methodology for Making Successful Logos
Benefits of Ruby On Rails Development
Benefits of Ruby On Rails Development
The Power of Colour and Shapes in Your Infant's Life.
The Power of Colour and Shapes in Your Infant's Life.
Advantages of Hiring PSD To HTML Service Providers
Advantages of Hiring PSD To HTML Service Providers
How to Make Responsive Web Design Attractive?
How to Make Responsive Web Design Attractive?
Converting PSD to Responsive HTML
Converting PSD to Responsive HTML
Just a few realy really hints and tips when it comes to website design but look for.
Just a few realy really hints and tips when it comes to website design but look for.

Leave Your Reply

*