【问题标题】:Nested Attributes in DelphiDelphi中的嵌套属性
【发布时间】:2011-08-24 09:55:14
【问题描述】:

有没有办法在 Delphi 中使用嵌套属性?目前我正在使用 Delphi XE。

例如:

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

而用法是

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

目前这会导致内部错误。这将是一个很好的功能,可以在属性上创建一些布尔表达式。

【问题讨论】:

  • 如果导致内部错误,显然没有办法。
  • 目前这似乎是真的。但问题是,这是否是设计使然。如果不是,这是一个错误吗?以后的版本会支持吗?
  • 我不知道这是否是设计使然。内部错误显然不是设计使然,但我不知道他们是否希望能够嵌套属性。只有 Embarcadero 的 Delphi 编译器团队可以回答这个问题。
  • Delphi 编译器团队似乎正忙于准备发布 XE2。我猜没时间了:)
  • 你能给出一些适合嵌套属性的情况吗?

标签: delphi attributes annotations nested


【解决方案1】:

我认为你的意思是创建方法的默认属性。

这样的事情应该可以工作:

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.

【讨论】:

  • 没有。 :-) 问题不在于default parameters;关于attributes。再次阅读示例用法;它与您的答案主题无关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-09
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多