【问题标题】:Attribute constructor with array parameter带数组参数的属性构造函数
【发布时间】:2015-01-13 18:14:05
【问题描述】:

今天我在这里重写了一些旧的东西,让自己陷入了一个我不知道答案的问题。

我创建了以下属性:

Enumeration<T> = class(TCustomAttribute)
strict private
    { Private declarations }
    FValues : TList<T>;
public
    { Public declarations }
    constructor Create(const AValues : array of T);
    destructor Destroy(); override;
public
    { Public declarations }
    property Values : TList<T> read FValues;
end;

考虑到这一点,我可以在以下类中很好地使用此属性,例如:

[Entity('tablename')]
TUser = class(TEntity)
strict private
   [Column('idcolumnname')]
   [PrimaryKey(True)]
   Fid : TInteger;

   [Column('typecolumnname')]
   [Enumeration<string>(['A', 'B', 'C', 'D', '...'])]
   Ftype: TEnumeration<string>;
end;

它工作得很好,但是 idk,在我看来这不应该工作,因为我的无知,delphi 属性只需要常量类型,而且我不仅使用数组作为参数,而且是通用的。

向前迈进,我做了这个属性:

Association = class(TCustomAttribute)
strict private
    { Private declarations }
    FMasterKeys : TList<string>;
    FDetailKeys : TList<string>;
public
    { Public declarations }
    constructor Create(const AMasterKeys, ADetailKeys : array of string);
    destructor Destroy(); override;
public
    { Public declarations }
    property MasterKeys : TList<string> read FMasterKeys;
    property DetailKeys : TList<string> read FDetailKeys;
end;

并尝试在此类上使用:

[Entity('tablename')]
TSuperUser = class(TEntity)
strict private
    [Association(['masterkey'], ['detailkey'])]
    Fuser : TAssociation<TUser>;
end;

我收到一个错误 [DCC 错误] E2026 常量表达式。

好的,所以在简历中我只是不知道为什么我可以使用 T 数组作为属性参数而不是字符串数组。

感谢您提前提供任何帮助

【问题讨论】:

标签: delphi delphi-xe2


【解决方案1】:

检查您的编译器警告。它应该为您的“编译代码”显示W1025 Unsupported language feature: 'custom attribute'。所以你虽然编译的实际上没有。它只是没有引发错误。

这通常是在无法找到属性类的情况下,因为事实上您不能拥有通用属性。在 XE7 中仍然如此。

底线:即使它确实编译了您的可执行文件也不会包含该属性。

【讨论】:

  • 是的,确实这条消息在输出中,但我不明白,我从输出文件夹中删除了应用程序 exe,尽管你说它实际上失败了,但编译再次构建它。那么实际上发生了什么?我的意思是编译输出成功的不受支持的功能它会导致什么?一个错误的可执行文件?!
  • 它不会包含该属性。
猜你喜欢
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
  • 2013-01-24
  • 2019-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-08
相关资源
最近更新 更多