Thursday, 21 March 2013

What is RTTI in Delphi ?.

Runtime Type Information is information about an object's data type that is set into memory at run-time.
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.
The is operator, which performs dynamic type checking, is used to verify the actual runtime class of an object ans as operator performs checked typecasts.

Ex.
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;

No comments:

Post a Comment