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

发布于 2024-11-10 04:03:31 字数 1199 浏览 3 评论 0原文

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

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

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

  • class and record fields and methods
  • method parameters
  • properties
  • non-local enumeration declarations
  • non-local variable declarations

Are there other language elements where attributes can be placed?


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

It contains this code example:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里°也失望 2024-11-17 04:03:31

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

  • 枚举
  • 函数类型
  • 过程类型
  • 方法类型(对象的
  • 别名类型
  • 记录类型
  • 类类型
  • 类内部的记录类型
  • 记录字段
  • 记录方法
  • 类实例字段
  • 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)
  • 方法类型 (< code>TRttiMethodType)
  • 指针类型 (TRttiPointerType) - 它的用途是什么?
  • 过程类型 (TRttiProcedureType)

无法检索“单元”级别或局部变量和过程的任何类型的 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:

  • Enums
  • Function type
  • Procedure type
  • Method type (of object)
  • Aliased type
  • Record type
  • Class type
  • Record type that's internal to a class
  • Record field
  • Record method
  • Class instance field
  • Class class field (class var)
  • Class method
  • Global variable
  • Global function
  • Local variable

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.

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

  • Record type (TRttiRecordType)
  • Instance type (TRttiInstanceType)
  • Method type (TRttiMethodType)
  • Pointer type (TRttiPointerType) - what's that used for?
  • Procedure type (TRttiProcedureType)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文