考慮下面的代碼片段:TCustomAttribute - 「常量表達式預期」編譯錯誤
type
MyIntf = interface
['{C6184693-663E-419F-B2DA-4DA1A0E33417}']
procedure Foo;
end;
InvisiblePropInterfaces = class(TCustomAttribute)
private
FGUIDS: array of TGUID;
public
constructor Create(const GUIDS: array of TGUID);
end;
[InvisiblePropInterfaces([MyIntf])] // <-- Constant expression expected error
TMyClass = class(TInterfacedObject, MyIntf)
procedure Foo;
end;
爲什麼編譯器認爲這不是一個常量表達式? 但是,鑑於我使用InvisiblePropInterfaces這樣,編譯器只是快樂?
...
var
I: InvisiblePropInterfaces;
begin
I:= InvisiblePropInterfaces.Create([MyIntf]);
...
似乎我運氣不佳,首次真正使用屬性。那麼,我不得不改變我的設計,只是用另一個接口來修飾我的類,如下所示:IInvisiblePropInterfaces =接口函數GetGuids:TGuid數組 – iamjoosy