RTTI(Runtime Type Information)は、Delphi 2010で慎重に再設計されました 。
RTTIは
Delphi IDEが書かれた中心的な要素であり、最初のリリース以来存在していますが、RTTIを使用しようとして、特に
Javaの Reflection APIに比べて複雑すぎて複雑であると長年聞いた人もい
ます。そして。
NET 事前にタイプを知らずに他のオブジェクトに関する詳細な情報を要求するコードを書く機能は非常に強力な機能であるため、これは非常に残念です。
ただし、新しい
APIでは苦情は過去のものになると思い
ます 。 拡張されただけでなく、はるかにアクセスしやすく使いやすくなりました。
私が非常に感銘を受けた新機能の1つは、
Win32環境での属性のサポートです。 私は素晴らしい例に取り組んでいますが、ここではカスタム属性の作成とリクエストについて簡単に説明します。
カスタム属性は、
TCustomAttributeを継承する単なるクラスです。 他のクラスと同様に、プロパティ、メソッドなどを持つことができます。
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