开发者

Nested Attributes in Delphi

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

For example:

TCo开发者_如何学CmpoundAttribute = 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.


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜