RTTI (Runtime Type Information) was carefully reworked in Delphi 2010 .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;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;
Source: https://habr.com/ru/post/85509/
All Articles