【问题标题】:Which language elements can be annotated using attributes language feature of Delphi?哪些语言元素可以使用 Delphi 的属性语言功能进行注释?
【发布时间】:2011-09-01 11:25:36
【问题描述】:

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

到目前为止,我发现的示例包括类声明、字段和方法。 (并且 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);

【问题讨论】:

  • +1 有趣的问题。文档声明属性是用于"annotating types and type members" - 我怀疑这意味着type 子句中的 anything 以及在记录或类中声明的任何内容(成员变量、属性、函数、过程,内部类等)
  • 关于属性的链接对我不起作用。如果您还记得它所说的内容,您介意编辑下面的答案以演示在属性上使用属性吗? (我找不到该链接的其他来源,因为引文不包含有关作者、主题、论坛甚至日期的任何内容。)
  • @RobKennedy 感谢您的关注!我找不到原始来源,但另一篇文章并更新了问题

标签: delphi annotations delphi-2010 delphi-xe delphi-xe2


【解决方案1】:

有趣的问题!您可以在几乎任何东西上声明属性,问题在于使用 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.

问题在于检索那些自定义属性。查看rtti.pas 单元,可以检索自定义属性:

  • 记录类型 (TRttiRecordType)
  • 实例类型 (TRttiInstanceType)
  • 方法类型 (TRttiMethodType)
  • 指针类型 (TRttiPointerType) - 它是做什么用的?
  • 程序类型 (TRttiProcedureType)

无法为“单元”级别或局部变量和过程检索任何类型的 RTTI,因此无法检索有关属性的信息。

【讨论】:

  • 不支持属性的属性很奇怪(而且很遗憾 - 属性是 Delphi 类中最棒的事情之一。)顺便说一句,+1 是一个很好的答案。
  • @David M:查看我的更新,看起来属性上的属性工作正常
  • 过程和函数参数呢,能有属性吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-07
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
  • 2010-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多