官术网_书友最值得收藏!

  • Expert Delphi
  • Pawe? G?owacki
  • 329字
  • 2021-07-02 20:44:24

Custom attributes

Custom attributes let you add custom information to a class itself or to its individual members. Many libraries that come with Delphi use custom attributes. They are useful when we want to mark certain types or type members that need to be treated in a special way. For example, in the DUnitX unit testing framework that comes with Delphi, we use the Test custom attributes to mark a method as testable and optionally provide parameters to be used by the unit test runner. Another example is the REST API resources, which are published from EMS modules. With custom attributes, we can tell the framework what should be the name of the REST resource to access a certain method.

A custom attribute is just a regular class that inherits from the TCustomAttribute type defined in a built-in system unit.

Let's consider the definition of a custom documentation attribute. A programmer could use such an attribute to associate a given class member in code with a custom URL, with more information about it. Take a look at the following code snippet:

unit uDocAttribute; 
 
interface 
 
type 
  DocAttribute = class(TCustomAttribute) 
  private 
    FURL: string; 
  public 
    constructor Create(URL: string); 
    property URL: string read FURL write FURL; 
  end; 
 
implementation 
 
{ DocAttribute } 
 
constructor DocAttribute.Create(URL: string); 
begin 
  FURL := URL; 
end; 

To apply an attribute, it needs to be placed just before the element it applies in square brackets. Optionally, it can take parameters that just come as a list in brackets. This will implicitly call the custom attribute constructor:

uses uDocAttribute; 
 
type 
  [Doc('http://mydocs/MySuperClass')] // skipping "...Attribute" 
  TMySuperClass = class 
  public 
    [DocAttribute('http://mydocs/MySuperClass/DoSomething')] 
    procedure DoSomething; 
  end; 

By convention, custom attribute class names end with Attribute. Delphi compiler allows us to skip the optional Attribute part of the attribute class name.

That's nice, but how do I know in my code that there are attributes applied to classes that my code operates on? This and many other capabilities are provided by the Runtime Type Information (RTTI) system.

主站蜘蛛池模板: 门源| 安康市| 抚远县| 水富县| 广饶县| 怀宁县| 洪泽县| 博客| 深圳市| 杭州市| 宝鸡市| 蕉岭县| 江门市| 青龙| 来凤县| 大方县| 天台县| 错那县| 泰州市| 茂名市| 札达县| 河津市| 紫金县| 澄迈县| 靖州| 乐安县| 阿坝县| 金昌市| 托里县| 阿拉善左旗| 石棉县| 论坛| 扬州市| 沙田区| 多伦县| 明光市| 福海县| 津南区| 中阳县| 高清| 三台县|