System.Rtti.TRttiObject.GetAttributes 简例

 1 MAttribute = class(TCustomAttribute)
 2   private
 3     FName: string;
 4   public
 5     constructor Create(AName: string);
 6   end;
 7   TMyClass = class
 8   private
 9     FProp2: Integer;
10     FProp1: string;
11     procedure SetProp1(const Value: string);
12     procedure SetProp2(const Value: Integer);
13   public
14     procedure Methd1(const A,B: string);
15     function Func1: Boolean;
16   published
17     [M('Prop1')]
18     property Prop1: string read FProp1 write SetProp1;
19     [M('Prop2')]
20     property Prop2: Integer read FProp2 write SetProp2;
21   end;
22 
23 procedure TForm1.btn1Click(Sender: TObject);
24 var
25   c: TRttiContext;
26   t: TRttiType;
27   ms: TArray<TRttiMethod>;
28   ps: TArray<TRttiProperty>;
29   fs: TArray<TRttiField>;
30   ats: TArray<TCustomAttribute>;
31   a : TCustomAttribute;
32   m: TRttiMethod;
33   p: TRttiProperty;
34   f: TRttiField;
35 begin
36   c := TRttiContext.Create;
37   t := c.GetType(TMyClass);
38   ms :=  t.GetMethods;
39   ps := t.GetProperties;
40   fs := t.GetFields;
41   ats := t.GetAttributes;
42 
43   for p in ps do
44   begin
45     for a in p.GetAttributes do
46     begin
47       if a is MAttribute then
48       begin
49         mmo1.Lines.Add(p.Name + ':' + MAttribute(a).FName);
50       end;
51     end;
52   end;
53 
54   mmo1.Lines.Add('-------------Methods-----------');
55   for m in ms do
56   mmo1.Lines.Add(m.Name);
57   mmo1.Lines.Add('------------Properties---------');
58   for p in ps do
59   mmo1.Lines.Add(p.Name);
60   mmo1.Lines.Add('--------------Fields-----------');
61   for f in fs do
62   mmo1.Lines.Add(f.Name);
63 
64 end;
原文地址:https://www.cnblogs.com/xspace/p/3627269.html