2011-05-25 26 views
29

Delphi 2010引入了可以添加到类型声明和方法的自定义属性。哪些语言元素可以使用自定义属性?哪些语言元素可以使用Delphi的属性语言功能进行注释?

到目前为止我发现的例子包括类声明,字段和方法。 (并且AFAIK泛型类不支持自定义属性)。

一些示例显示在this article中。它看起来像变量(任何类声明的外部)也可以有属性。

基于关于本文的,属性可用于

  • 类和记录字段和方法
  • 方法参数
  • 性质
  • 非局部枚举声明
  • 非局部变量声明

是否有可以放置属性的其他语言元素?


更新:此文章指出,自定义属性可以属性之前被放置: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); 
+2

+1有趣的问题。该文档指出属性用于[“注释类型和类型成员”](http://docwiki.embarcadero.com/RADStudio/en/Overview_of_Attributes) - 我怀疑这意味着'type'子句中的* nothing *因为无论在记录或类(成员变量,属性,函数,过程,内部类等等)中声明 – 2011-05-25 06:13:33

+0

关于属性的链接对我来说不起作用。如果你记得它说的话,你会介意编辑下面的答案来演示属性的使用属性吗? (我无法找到该链接的另一个来源,因为引用不包括关于作者,主题,论坛或甚至是日期的任何内容。) – 2016-07-22 16:16:04

+0

@RobKennedy感谢您注意!我找不到原始资料,但另一篇文章,并更新了问题 – mjn 2016-07-23 10:42:47

回答

24

有趣的问题!你可以在上声明属性几乎任何东西,问题是使用RTTI检索它们。以下是声明的自定义的快捷控制台演示属性:

  • 枚举
  • 功能型
  • 程序类型
  • 方法类型(of object
  • 锯齿型
  • 记录类型
  • 类键入
  • 类别内部的记录类型
  • 记录字段
  • 记录方法
  • 类的实例字段
  • class字段(class var
  • 类方法
  • 全局变量
  • 全局功能
  • 局部变量

没有找到一种方法来声明一个类的property的自定义属性。但是可以将自定义属性附加到getter或setter方法。

代码,故事中的代码之后继续说:

program Project25; 

{$APPTYPE CONSOLE} 

uses 
    Rtti; 

type 
    TestAttribute = class(TCustomAttribute); 

    [TestAttribute] TEnum = (first, second, third); 
    [TestAttribute] TFunc = function: Integer; 
    [TestAttribute] TEvent = procedure of object; 
    [TestAttribute] AliasInteger = Integer; 

    [TestAttribute] ARecord = record 
    x:Integer; 
    [TestAttribute] RecordField: Integer; 
    [TestAttribute] procedure DummyProc; 
    end; 

    [TestAttribute] AClass = class 
    strict private 
    type [TestAttribute] InnerType = record y:Integer; end; 
    private 
    [TestAttribute] 
    function GetTest: Integer; 
    public 
    [TestAttribute] x: Integer; 
    [TestAttribute] class var z: Integer; 
    // Can't find a way to declare attribute for property! 
    property Test:Integer read GetTest; 
    [TestAttribute] class function ClassFuncTest:Integer; 
    end; 

var [TestAttribute] GlobalVar: Integer; 

[TestAttribute] 
procedure GlobalFunction; 
var [TestAttribute] LocalVar: Integer; 
begin 
end; 

{ ARecord } 

procedure ARecord.DummyProc; 
begin 
end; 

{ AClass } 

class function AClass.ClassFuncTest: Integer; 
begin 
end; 

function AClass.GetTest: Integer; 
begin 
end; 

begin 
end. 

麻烦的是检索这些自定义属性。什么是 -

  • 记录类型(TRttiRecordType
  • 实例类型(TRttiInstanceType
  • 方法类型(TRttiMethodType
  • 指针类型(TRttiPointerType):纵观rtti.pas单元,自定义属性可以被检索用于?
  • 程序类型(TRttiProcedureType

有没有获取任何形式的RTTI的“单位”级或局部变量和程序,因此没有检索有关属性的信息的方式的方式。

+2

不支持属性属性很奇怪(而且遗憾 - 属性是关于Delphi类的令人敬畏的事情之一。)顺便说一句,+1是一个很好的答案。 – 2011-05-26 05:31:09

+2

@David M:看到我的更新,它看起来像属性上的属性工作正常 – mjn 2011-05-31 12:17:47

+0

过程和函数参数怎么样,他们可以有属性? – mjn 2012-01-31 09:57:09

相关问题