delphi - Delphi的属性语言特性可以注释哪些语言元素?

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

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

到目前为止我找到的例子包括类声明、字段和方法。 (据我所知泛型类不支持自定义属性)。

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);

最佳答案

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

  • 枚举
  • 函数类型
  • 程序类型
  • 方法类型(对象)
  • 别名类型
  • 记录类型
  • 类别类型
  • 类内部的记录类型
  • 记录字段
  • 记录方式
  • 类实例字段
  • class字段(class var)
  • 类方法
  • 全局变量
  • 全局函数
  • 局部变量

没有找到为类的属性声明自定义属性的方法。但自定义属性可以附加到 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 - Delphi的属性语言特性可以注释哪些语言元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6119986/

相关文章:

delphi - 如何同步 3 个 radio 组(不处理 3 个 OnClick()S)?

mysql - Delphi - 将 DateTime Null 保存到 DB (ADO/MySQL)

java - 将逻辑应用于 Spring @Import 注解

delphi - 将 5 个 ListBox 同步在一起

xml - delphi 7 读取和处理 xml 文件的方式和组件 - 更新

delphi - Listview 点击事件 (Delphi)

java - maven-compiler-plugin 3.6.0 不编译从注释生成的源

c - 这些函数和参数注释是什么?

delphi - 优势 5400 AE_INTERNAL_ERROR

delphi - 如何用线段创建二维圆弧的近似值?