Nested Attributes in Delphi

拈花ヽ惹草 提交于 2019-12-10 22:08:36

问题


Is there a way to use nested attributes in Delphi? At the moment I'm using Delphi XE.

For example:

TCompoundAttribute = class (TCustomAttribute)
public
  constructor Create (A1, A2 : TCustomAttribute)
end;

And the usage would be

[ TCompoundAttribute (TSomeAttribute ('foo'), TOtherAttribute ('bar')) ]

At the moment this leads to an internal error. This would be a nice feature to create some boolean expressions on attributes.


回答1:


I think you mean default attributes of create method.

Something like this should be working:

unit Unit1;
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TFoo = class
  private
    FA1: string;
    FA2: string;
    { Private declarations }
  public
    procedure Show;
    constructor Create (a1: string = 'foo'; a2: string = 'bar');
  end;

var
  o : Tfoo;

implementation

{$R *.dfm}

procedure Tfoo.show;
begin
  ShowMessage(FA1 + ' ' + FA2);
end;

constructor Tfoo.create (a1: string = 'foo'; a2: string = 'bar');
begin
  FA1 := a1;
  FA2 := a2;
end;


begin
  o := Tfoo.create;
  o.show;   //will show 'foo bar'
  o.Free;

  o := Tfoo.create('123');
  o.show;   //will show '123 bar'
  o.Free;

  o := Tfoo.create('123', '456');
  o.show;   //will show '123 456'
  o.Free;

  //etc..
end.


来源:https://stackoverflow.com/questions/7173496/nested-attributes-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!