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

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

问题描述

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



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



有些示例显示在这篇文章。它看起来像变量(外部的任何类声明)也可以有属性。



根据这篇文章,属性可以用于




  • 类和记录字段和方法

  • 方法参数

  • 属性

  • 非本地枚举声明

  • 非局部变量声明



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






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



它包含此代码示例:

  type 
TConfig = class(TComponent)
public
[PersistAs('Config','Version','1.0')]
版本:
[PersistAs('Config','Description','No description')]
说明:String;
FTest:整数;
// No attribute =>不持久
计数:整数;
[PersistAs('Config','Test','0')]
属性测试:整数读取FTest写入FTest;
结束






我想还有一种方法来阅读方法参数的属性,如

  procedure Request([FormParam] AUsername:string; [FormParam] APassword:string); 


解决方案

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




  • 枚举

  • 功能类型

  • 过程类型

  • 方法类型(对象

  • 别名类型

  • 记录类型

  • 类类型

  • 类内部的记录类型

  • 记录字段

  • 记录方法

  • 类实例字段

  • class field( class var

  • 类方法

  • 全局变量

  • 全局函数

  • 本地变量



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



代码,故事在代码之后继续:

 程序Project25; 

{$ APPTYPE CONSOLE}

使用
Rtti;

type
TestAttribute = class(TCustomAttribute);

[TestAttribute] TEnum =(第一,第二,第三);
[TestAttribute] TFunc = function:Integer;
[TestAttribute] TEvent =对象的过程;
[TestAttribute] AliasInteger =整数;

[TestAttribute] ARecord = record
x:整数;
[TestAttribute] RecordField:Integer;
[TestAttribute]程序DummyProc;
结束

[TestAttribute] AClass = class
strict private
type [TestAttribute] InnerType =记录y:整数;结束;
private
[TestAttribute]
函数GetTest:Integer;
public
[TestAttribute] x:整数;
[TestAttribute] class var z:Integer;
//找不到声明属性属性的方法!
属性测试:整数读取GetTest;
[TestAttribute]类函数ClassFuncTest:Integer;
结束

var [TestAttribute] GlobalVar:Integer;

[TestAttribute]
程序GlobalFunction;
var [TestAttribute] LocalVar:Integer;
begin
end;

{ARecord}

过程ARecord.DummyProc;
begin
end;

{AClass}

类函数AClass.ClassFuncTest:Integer;
begin
end;

function AClass.GetTest:Integer;
begin
end;

begin
end。

麻烦的是检索这些自定义属性。查看 rtti.pas 单位,可以检索自定义属性:




  • 记录类型( TRttiRecordType

  • 实例类型( TRttiInstanceType

  • 方法类型( TRttiMethodType

  • 指针类型( TRttiPointerType ) - 用于什么?

  • 过程类型( TRttiProcedureType



    • 没有办法为单位级别或本地变量和过程检索任何类型的RTTI,因此无法检索有关属性的信息。


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

      解决方案

      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.

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

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