Attribute constructor with array parameter

I was rewriting some old stuff here today and got into a problem and I don't know the answer.

i created the following attribute:

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;

      

with that in mind, I can use this attribute just fine in the following class, for example:

[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;

      

Great that it worked, but idk, it seems to me that this shouldn't work, in my ignorance, delphi attributes only expect constant types, and im not only using an array as a parameter but also a generic one.

Moving foward, I made this attribute:

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;

      

and tried to use in this class:

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

      

I got [DCC Error] E2026 Constant expression expected.

ok, so in the summary, I just don't know what is going on, why can I use an array T as an attribute parameter and not a string array, eg.

thanks for any help in advance

+3


source to share


1 answer


Check compiler warnings. It should say W1025 Unsupported language feature: 'custom attribute'

for your "compilation code". So, what you compile though was not really there. It just didn't throw an error.

This usually happens when the class of an attribute cannot be found because you cannot have common attributes. This is still the case in XE7.



Bottom line: even if it compiled your executable, this attribute won't contain.

+5


source







All Articles