哪些语言元素可以使用 Delphi 的属性语言特性进行注释? [英] Which language elements can be annotated using attributes language feature of Delphi?

查看:27
本文介绍了哪些语言元素可以使用 Delphi 的属性语言特性进行注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Delphi 2010 introduced custom attributes which can be added to type declarations and methods. For which language elements can a custom attribute be used?

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

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

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

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

Are there other language elements where attributes can be placed?

更新:这篇文章表明自定义属性可以放在属性之前:http://francois-piette.blogspot.de/2013/01/using-custom-attribute-for-data.html

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

它包含此代码示例:

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

推荐答案

有趣的问题!您可以在几乎任何东西上声明属性,问题是使用 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 class 字段(class var)
  • 类方法
  • 全局变量
  • 全局函数
  • 局部变量

没有找到为类的 property 声明自定义属性的方法.但是自定义属性可以附加到 getter 或 setter 方法.

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:

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 单元,可以检索自定义属性:

The trouble is retrieving those custom attributes. Looking at the rtti.pas unit, custom attributes can be retrieved for:

  • 记录类型(TRttiRecordType)
  • 实例类型 (TRttiInstanceType)
  • 方法类型(TRttiMethodType)
  • 指针类型 (TRttiPointerType) - 用来做什么?
  • 过程类型(TRttiProcedureType)
  • Record type (TRttiRecordType)
  • Instance type (TRttiInstanceType)
  • Method type (TRttiMethodType)
  • Pointer type (TRttiPointerType) - what's that used for?
  • Procedure type (TRttiProcedureType)

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

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.

这篇关于哪些语言元素可以使用 Delphi 的属性语言特性进行注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆