-
Notifications
You must be signed in to change notification settings - Fork 1
/
YWRTLUtils.pas
156 lines (144 loc) · 3.61 KB
/
YWRTLUtils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
unit YWRTLUtils;
interface
type
TAttributeType = class of TCustomAttribute;
function GetStoredAttributeNames(T: TClass) : TArray<String>;
function GetClassesWithAttributes(T : TClass; S : String): TArray<TClass>; overload;
function GetClassesWithAttributes(T : TClass; S : TAttributeType): TArray<TClass>; overload;
function GetAttribute(T : TClass; S : TAttributeType): TCustomAttribute;
function GetAttributes(T : TClass; S : TAttributeType): TArray<TCustomAttribute>;
implementation
uses classes, sysutils, System.Rtti, Generics.Collections;
function GetAttribute(T : TClass; S : TAttributeType): TCustomAttribute;
var C : TRttiContext;
I : TRttiType;
A : TCustomAttribute;
begin
Result := nil;
C := TRttiContext.Create;
try
I := C.GetType(T);
for A in I.GetAttributes do
begin
if A is S then begin
Result := A;
exit;
end;
end;
finally
C.Free;
end;
end;
function GetAttributes(T : TClass; S : TAttributeType): TArray<TCustomAttribute>;
var C : TRttiContext;
I : TRttiType;
A : TCustomAttribute;
N : integer;
begin
SetLength(Result,20);
N := 0;
C := TRttiContext.Create;
try
I := C.GetType(T);
for A in I.GetAttributes do
begin
if A is S then begin
if N=Length(Result) then SetLength(Result,Length(Result)+20);
Result[N] := A;
inc(N);
end;
end;
finally
C.Free;
end;
SetLength(Result,N);
end;
function GetClassesWithAttributes(T : TClass; S : String): TArray<TClass>;
var C : TRttiContext;
I : TRttiType;
A : TCustomAttribute;
S1,S2 : integer;
begin
S := S.ToUpper.Trim;
C := TRttiContext.Create;
S1 := 0; S2 := 0;
try
for I in C.GetTypes do
begin
if I.IsInstance and I.AsInstance.MetaclassType.InheritsFrom(T) then begin
for A in I.GetAttributes do
begin
if (A is StoredAttribute)and(StoredAttribute(A).Name.ToUpper.Trim=S)
then begin
if S1=S2 then begin
inc(s2,20);
setlength(Result,s2);
end;
Result[S1] := I.AsInstance.MetaclassType;
inc(S1);
end;
end;
end;
end;
setlength(Result,s1);
finally
C.Free;
end;
end;
function GetClassesWithAttributes(T : TClass; S : TAttributeType): TArray<TClass>;
var C : TRttiContext;
I : TRttiType;
A : TCustomAttribute;
S1,S2 : integer;
begin
C := TRttiContext.Create;
S1 := 0; S2 := 0;
try
for I in C.GetTypes do
begin
if I.IsInstance and I.AsInstance.MetaclassType.InheritsFrom(T) then begin
for A in I.GetAttributes do
begin
if A is S then begin
if S1=S2 then begin
inc(s2,20);
setlength(Result,s2);
end;
Result[S1] := I.AsInstance.MetaclassType;
inc(S1);
end;
end;
end;
end;
setlength(Result,s1);
finally
C.Free;
end;
end;
function GetStoredAttributeNames(T:TClass) : TArray<String>;
var C : TRttiContext;
I : TRttiType;
A : TCustomAttribute;
S1,S2 : integer;
begin
C := TRttiContext.Create;
s1 := 0; s2 := 0;
try
I := C.GetType(T);
for A in I.GetAttributes do
begin
if A is StoredAttribute then begin
if s1=s2 then begin
inc(s2,20);
setlength(Result,s2);
end;
Result[s1] := StoredAttribute(A).Name;
inc(s1);
end;
end;
SetLength(Result,s1);
finally
C.Free;
end;
end;
end.