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

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

问题描述

德尔福2010年推出它可以添加到类型声明和方法的自定义属性。对于语言要素都可以自定义属性可以使用?

这是我迄今为止发现的例子包括类声明,字段和方法。 (和AFAIK泛型类不支持自定义属性)。

一些例子本文所示。它看起来像变量(外部任何类的声明),也可以有属性。

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


  • 类和记录字段和方法

  • 方法参数

  • 属性

  • 非本地枚举声明

  • 非本地变量声明

在哪里属性可以放在那里等语言元素?


更新:此线程显示自定义属性可以属性之前,放置并阅读他们的RTTI是可能的:<一href=\"http://meinews.niuz.biz/re-t658747.html?s=b7e510a70882e974d008978837e7ca37\">http://meinews.niuz.biz/re-t658747.html?s=b7e510a70882e974d008978837e7ca37

我想这也有一个方法来读取方法的参数属性,如

 程序请求([FormParam] AUsername:字符串; FormParam] APassword:字符串);


解决方案

有趣的问题!您可以在几乎所有的东西声明属性,这个问题是使用RTTI获取它们。下面是声明定制的快速控制台演示属性:


  • 枚举

  • 功能型

  • 程序类型

  • 方法的类型(对象

  • 锯齿型

  • 记录类型

  • 类类型

  • 记录类型的内部对类

  • 记录字段

  • 录制方法

  • 类的实例字段

  • 字段(类变种

  • 类方法

  • 全局变量

  • 全局函数

  • 局部变量

没有找到一个方法来声明一个类的属性自定义属性。但自定义属性可以附着到吸气剂或setter方法​​

code,故事延续了code后:

 程序Project25;{$ APPTYPE CONSOLE}用途
  RTTI;类型
  TestAttribute =类(TCustomAttribute);  [TestAttribute] TEnum =(第一,第二,第三);
  [TestAttribute] TFunc =功能:整数;
  [TestAttribute] TEvent =对象的程序;
  [TestAttribute] Alias​​Integer =整数;  [TestAttribute] =的arecord纪录
    X:整数;
    [TestAttribute] RecordField:整数;
    [TestAttribute]程序DummyProc;
  结束;  [TestAttribute] ACLASS =类
  严格的私人
    输入[TestAttribute] InnerType =记录Y:整数;结束;
  私人的
    [TestAttribute]
    功能GetTest:整数;
  上市
    [TestAttribute]×:整数;
    [TestAttribute]类变种Z:整数;
    //无法找到一个方法来声明属性的财产!
    性能试验:整数读取GetTest;
    [TestAttribute]类功能ClassFuncTest:整数;
  结束;VAR [TestAttribute] GlobalVar:整数;[TestAttribute]
程序GlobalFunction;
VAR [TestAttribute] localVar的:整数;
开始
结束;{ 一个记录 }程序ARecord.DummyProc;
开始
结束;{ 一类 }类函数AClass.ClassFuncTest:整数;
开始
结束;功能AClass.GetTest:整数;
开始
结束;开始
结束。

麻烦的是检索这些自定义属性。综观 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 thread shows that custom attributes can be placed before properties and reading their RTTI is possible: http://meinews.niuz.biz/re-t658747.html?s=b7e510a70882e974d008978837e7ca37

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天全站免登陆