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);
type
子句中的任何内容,以及记录或类内声明的任何内容(成员变量、属性、函数、过程、内部类等)。 - David