RTTI (Runtime Type Information) was carefully reworked in Delphi 2010 .
RTTI is the central element on which the
Delphi IDE is written, it has existed since the first release, however I have heard from some people over the years that they tried to use RTTI and found it too complex and complicated, especially compared to the
Reflection API in
Java and
NET . This is a real shame, because the ability to write code to request detailed information about other objects, without knowing their type in advance, is a really powerful feature.
However, I think complaints will be a thing of the past with the new
API . It was not only expanded, but became much more accessible and easy to use.
One of the new features that I am very impressed with is the support of attributes in the
Win32 environment. I am working on a great example, but here we will quickly look at creating and then querying custom attributes.
Custom attributes are simply classes that are inherited from
TCustomAttribute . They can have properties, methods, etc., like any other class:
MyAttribute = class(TCustomAttribute)
private
FName: string;
FAge: Integer;
public
constructor Create(const Name : string; Age : Integer);
property Name : string read FName write FName;
property Age : Integer read FAge write FAge;
end;
, .
:
TMyClass = class
public
[MyAttribute('Malcolm', 39)]
procedure MyProc(const s : string);
[MyAttribute('Julie', 37)]
procedure MyOtherProc;
end;
, . . , , , .
, , -, ,
API RTTI ,
ListBox:
procedure TForm3.Button1Click(Sender: TObject);
var
ctx : TRttiContext;
t : TRttiType;
m : TRttiMethod;
a : TCustomAttribute;
begin
ListBox1.Clear;
ctx := TRttiContext.Create;
try
t := ctx.GetType(TMyClass);
for m in t.GetMethods do
for a in m.GetAttributes do
if a is MyAttribute then
ListBox1.Items.Add(Format('Method = %s; Attribute = %s, Name = %s, Age = %d',
[m.Name, a.ClassName, MyAttribute(a).Name,
MyAttribute(a).Age]));
finally
ctx.Free;
end;
end;
RTTI , ClassType .
for..in ,
for..in . , ,
Name Age,
ListBox.

, , , , ,
Delphi 2010.
.translated.by/you/rtti-and-attributes-in-delphi-2010/into-ru: r3code