Delphi的属性语言特性可以注释哪些语言元素?
Delphi 2010 引入了可以添加到类型声明和方法中的自定义属性。自定义属性可以用于哪些语言元素?
到目前为止我找到的例子包括类声明、字段和方法。 (据我所知通用类不支持自定义属性)。
本文中显示了一些示例。看起来变量(任何类声明外部的)也可以有属性。
根据本文,属性可用于
- 类和记录字段以及方法方法
- 参数
- 属性
- 非局部枚举声明
- 非局部变量声明
是否还有其他可以放置属性的语言元素?
更新:本文表明自定义属性可以放置在属性之前: http://francois-piette.blogspot.de/2013/01/using-custom-attribute-for-data.html
它包含此代码示例:
type
TConfig = class(TComponent)
public
[PersistAs('Config', 'Version', '1.0')]
Version : String;
[PersistAs('Config', 'Description', 'No description')]
Description : String;
FTest : Integer;
// No attribute => not persistent
Count : Integer;
[PersistAs('Config', 'Test', '0')]
property Test : Integer read FTest write FTest;
end;
我想还有一种阅读方法方法参数上的属性,例如
procedure Request([FormParam] AUsername: string; [FormParam] APassword: string);
Delphi 2010 introduced custom attributes which can be added to type declarations and methods. For which language elements can a custom attribute be used?
The examples which I have found so far include class declarations, fields and methods. (And AFAIK generic classes do not support custom attributes).
Some examples are shown in this article. It looks like variables (external to any class declaration) also can have attributes.
Based on this article, attributes can be used for
- class and record fields and methods
- method parameters
- properties
- non-local enumeration declarations
- non-local variable declarations
Are there other language elements where attributes can be placed?
Update: this article indicates that custom attributes can be placed before properties: http://francois-piette.blogspot.de/2013/01/using-custom-attribute-for-data.html
It contains this code example:
type
TConfig = class(TComponent)
public
[PersistAs('Config', 'Version', '1.0')]
Version : String;
[PersistAs('Config', 'Description', 'No description')]
Description : String;
FTest : Integer;
// No attribute => not persistent
Count : Integer;
[PersistAs('Config', 'Test', '0')]
property Test : Integer read FTest write FTest;
end;
I guess that there is also a way to read attributes on method arguments like
procedure Request([FormParam] AUsername: string; [FormParam] APassword: string);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有趣的问题!您可以在几乎任何东西上声明属性,问题是使用 RTTI 检索它们。以下是声明自定义属性的快速控制台演示:
)
class
字段 (class var
)没有找到为
property
声明自定义属性的方法一堂课。但自定义属性可以附加到 getter 或 setter 方法。代码,代码之后故事继续:
麻烦在于检索那些自定义属性。查看
rtti.pas
单元,可以检索以下自定义属性:TRttiRecordType
)TRttiInstanceType
)TRttiPointerType
) - 它的用途是什么?TRttiProcedureType
)无法检索“单元”级别或局部变量和过程的任何类型的 RTTI,因此无法检索有关属性的信息。
Interesting question! You can declare attributes on almost anything, the problem is retrieving them using RTTI. Here's a quick console demo of declaring custom attributes for:
of object
)class
field (class var
)Didn't find a way to declare a custom attribute for a
property
of a class. But a custom attribute can be attached to the getter or setter methods.Code, the story continues after the code:
The trouble is retrieving those custom attributes. Looking at the
rtti.pas
unit, custom attributes can be retrieved for:TRttiRecordType
)TRttiInstanceType
)TRttiMethodType
)TRttiPointerType
) - what's that used for?TRttiProcedureType
)There's no way of retrieving any sort of RTTI for "unit" level or local variables and procedures, hence no way of retrieving information about attributes.