Sign in Join
Wiki copytemporary

Custom Attributes/zh CN: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11自定义属性(Custom Attributes)目前允许您为类型定义和类的已发布属性添加额外的元数据,这些元数据可以通过RTTI(运行时类型信息)进行查询。
22
33**截至2020年10月,该功能仅在FPC主干版本(即3.3.1)中可用**
44
5-## Contents
6-
7- * 1 属性可以用来做什么?
8- * 2 如何声明属性?
9- * 3 如何使用属性?
10- * 3.1 查询属性
11- * 4 属性特性与Delphi的兼容性
12- * 5 完整示例
13-
145## 属性可以用来做什么?
156
167属性用于将特定的元数据与类相关联。例如,您可以使用属性来标记一个类,指定其对应的数据库表名,或者为Web服务类添加一个标识其基本路径的字符串。
178
189## 如何声明属性?
1910
2011属性只是从新的系统类型`TCustomAttribute`派生的类。实现此类`TCustomAttribute`后代的构造函数是最重要的特性,因为它们用于向属性传递额外的参数(如上述示例中的关联数据库表名或基本路径)。
2112
2213**重要** :如果您想在不使用任何参数的情况下使用您的属性类型,则必须自己声明一个无参数的构造函数,因为`TCustomAttribute`的构造函数是私有的。
2314
2415## 如何使用属性?
2516
2617通过将至少一个属性子句放在类型或属性之前,可以将属性绑定到类型或属性。对于类型,属性在类型定义(如类、记录或枚举声明)或唯一类型重声明(例如"TLongInt = type LongInt")中指定。仅允许类型重声明,不允许仅重命名类型(例如"TLongInt = LongInt")。
2718
2819如果设置了新的模式开关`PREFIXEDATTRIBUTES`(在Delphi和DelphiUnicode模式下是默认的),则可用属性子句。
2920
3021属性子句的语法如下:
3122```
3223ATTRIBUTECLAUSE::='[' ATTRIBUTELIST ']'
3324 ATTRIBUTELIST::=ATTRIBUTE [, ATTRIBUTELIST ]
3425 ATTRIBUTE::=IDENTIFIER [ ( PARAMLIST ) ]
3526 PARAMLIST::=CONSTEXPR [, PARAMLIST ]
3627```
3728
3829其中,`IDENTIFIER`是属性类的名称。如果属性类的名称以"Attribute"结尾(不区分大小写),则后续可以省略"Attribute"后缀。因此,在以下示例中,`TMyAttribute`和`TMy`是等效的替代方案:
3930```
4031program tcustomattr;
4132
4233 {$mode objfpc}{$H+}$
4334 {$modeswitch prefixedattributes}
4435
4536 type
4637 TMyAttribute = class(TCustomAttribute)
4738 constructor Create;
4839 constructor Create(aArg: String);
4940 constructor Create(aArg: TGUID);
5041 constructor Create(aArg: LongInt);
5142 end;
5243
5344 {$M+}
5445 [TMyAttribute]
5546 TTestClass = class
5647 private
5748 fTest: LongInt;
5849 published
5950 [TMyAttribute('Test')]
6051 property Test: LongInt read fTest;
6152 end;
6253 {$M-}
6354
6455 [TMyAttribute(1234)]
6556 [TMy('Hello World')]
6657 TTestEnum = (
6758 teOne,
6859 teTwo
6960 );
7061
7162 [TMyAttribute(IInterface), TMy(42)]
7263 TLongInt = type LongInt;
7364
7465 constructor TMyAttribute.Create;
7566 begin
7667 end;
7768
7869 // ... 其他构造函数实现 ...
7970
8071 constructor TMyAttribute.Create(aArg: String);
8172 begin
8273 end;
8374
8475 constructor TMyAttribute.Create(aArg: LongInt);
8576 begin
8677 end;
8778
8879 constructor TMyAttribute.Create(aArg: TGUID);
8980 begin
9081 end;
9182
9283 begin
9384
9485
9586 begin
9687
9788 end.
9889```
9990
10091### 查询属性
10192
10293属性可以通过TypInfo和Rtti单元进行访问。
10394
10495对于TypInfo单元,访问属性的方式如下:
10596
10697对于类型:
10798
10899 * 使用`TTypeData`中的`AttributesTable`字段、
109100 * 对`PTypeInfo`使用`GetAttributeTable`、
110101 * 对属性表使用`GetAttribute`结合索引获取`TCustomAttribute`实例。
111102
112103对于属性:
113104
114105 * 使用`TPropInfo`的`AttributesTable`、
115106 * 对属性表使用`GetAttribute`结合索引获取`TCustomAttribute`实例、
116107 * 对`PPropInfo`使用`GetPropAttribute`结合索引获取`TCustomAttribute`实例。
117108
118109对于Rtti单元,访问属性的方式如下:
119110
120111对于类型:
121112
122113 * 对目标类型的`TRttiType`使用`GetAttributes`。
123114
124115对于属性:
125116
126117 * 对目标属性的`TRttiProperty`使用`GetAttributes`。
127118
128119## 属性特性与Delphi的兼容性
129120
130121该特性本身与Delphi兼容,但FPC对未绑定属性的容忍度较低。如果属性类未知或属性子句未绑定到有效的类型或属性,编译器将生成错误。
131122
132123FPC的RTTI不被认为是与Delphi兼容的,但它涵盖了相同的功能。与Delphi(使用Invoke创建属性实例)不同,FPC使用构造函数函数。FPC的实现优势在于它可以在没有完整Invoke支持的系统上工作。
133124
134125此外,使用`PREFIXEDATTRIBUTES`模式开关会禁用函数、方法和过程/方法类型的指令子句。启用`PREFIXEDATTRIBUTES`模式开关后,以下用法不再允许:
135126```
136127procedure Test; [cdecl];
137128 begin
138129
139130 end;
140131```
141132
142133## 完整示例
143134```
144135program testattributes;
145136 {
146137 这是一个简单的示例,说明如何使用自定义属性。
147138 该类使用自定义属性在程序启动时检索静态日期。这仅用于演示。
148139 此演示包含:
149140 - 创建
150141 - 装饰
151142 - 检索
152143 }
153144
154145 {$mode delphi}{$H+}{$M+}
155146 {$warn 5079 off} { 关闭实验性警告 }
156147 uses
157148 sysutils, typinfo, rtti, classes;
158149
159150 type
160151 { 用于装饰类以指定特定日期的自定义属性 }
161152 ADateTimeAttribute = class(TCustomAttribute)
162153 private
163154 FArg:TDateTime;
164155 public
165156 { 展示自定义属性可以有多个构造函数 }
166157 constructor Create(const aArg: TDateTime);overload;
167158 { 我们可以使用预定义的%DATE%编译器包含
168159 在自定义属性的上下文中,我们需要一个
169160 常量表达式作为默认参数 }
170161 constructor Create(const aArg: String = {$I %DATE%});overload;
171162 published
172163 property Date:TDateTime read FArg;
173164 end;
174165
175166 { 使用我们的自定义属性装饰的日期时间类 }
176167 { 注意:您可以省略'Attribute',编译器会解析它 }
177168 { [ADateTime(21237.0)] 调用第一个构造函数,显示1958年的某个日期 }
178169
179170 { 这将调用第二个构造函数 }
180171 [ADateTime]
181172 TMyDateTimeClass = class
182173 private
183174 FDateTime:TDateTime;
184175 public
185176 constructor create;
186177 published
187178 [ADateTime]
188179 property Day:TDateTime read FDateTime write FDateTime;
189180 end;
190181
191182 constructor ADateTimeAttribute.Create(const aArg: TDateTime);
192183 begin
193184 FArg := aArg;
194185 end;
195186
196187 constructor ADateTimeAttribute.Create(const aArg: string );
197188 var
198189 MySettings:TFormatSettings;
199190 begin
200191 { 根据编译器如何格式化%DATE%包含来设置日期格式 }
201192 MySettings := DefaultFormatSettings;
202193 MySettings.ShortDateFormat:='yyyymmdd';
203194 MySettings.DateSeparator :='/';
204195 { 现在进行转换 }
205196 FArg := StrToDateTime(aArg, MySettings);
206197 end;
207198
208199 { 我们查询rtti来设置值 }
209200 constructor TMyDateTimeClass.Create;
210201 var
211202 Context : TRttiContext;
212203 AType : TRttiType;
213204 Attribute : TCustomAttribute;
214205 begin
215206 inherited;
216207 Context := TRttiContext.Create;
217208 try
218209 AType := Context.GetType(TypeInfo(TMyDateTimeClass));
219210 for Attribute in AType.GetAttributes do begin
220211 if Attribute is ADateTimeAttribute then
221212 FDateTime := ADateTimeAttribute(Attribute).Date;
222213 end;
223214 finally
224215 Context.Free
225216 end;
226217 end;
227218
228219 var
229220 Test:TMyDateTimeClass;
230221 begin
231222 Test := TMyDateTimeClass.Create;
232223 try
233224 writeln('Compile date is :',DateTimeToStr(Test.Day));
234225 finally
235226 Test.Free;
236227 end;
237228 end.
238229```
239230
240231---
241232*From the Free Pascal wiki, page "Custom Attributes/zh CN", as the Internet Archive kept it on 2025-05-16, last changed on 2024-07-13. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history