考虑以下(无法编译)代码:
program AttributesTestProgram; {$APPTYPE CONSOLE} uses SysUtils,Classes,RTTI; type TDisplayTextAttribute = class(TCustomAttribute) private FDisplayText: string; public constructor Create(aDisplayText: string); property DisplayText: string read FDisplayText write FDisplayText; end; constructor TDisplayTextAttribute.Create(aDisplayText: string); begin FDisplayText := aDisplayText; end; function GetFirstName: string; begin Result := 'First Name'; end; type TCustomer = Class(TObject) private FFirstName: string; FLastName: string; FStreetAddress: string; FZIP: string; FState: string; FCity: string; FPhone: string; published [TDisplayTextAttribute(GetFirstName)] property FirstName: string read FFirstName write FFirstName; end; begin // Code that does the work removed for clarity.... Readln; end.
我很自然地想知道为什么这个错误无法编译:
[DCC Error] AttributesTestProgram.dpr(40): E2026 Constant expression expected
我认为它与属性必须在编译器时间绑定的想法有关,或者沿着那些行绑定.
因此,我的问题是这样的:
有没有办法在这里“击败系统”并获得属性中的那个点的运行时值?