Delphi 11.3中自定义Field属性标注字段编译失败求助
Delphi 11.3自定义Attribute标注字段编译错误解决方法
你的问题出在自定义Attribute的使用语法上,当Attribute类没有构造参数时,标注时不能加括号。
错误原因
Delphi中,无参数的Attribute标注时不需要带括号,[Field()]的写法违反了编译器的语法规则,因此触发E2029 ':' expected but '(' found错误。
修正后的代码
Field = class(TCustomAttribute) end; TUser = class private [Field] // 去掉括号即可正常编译 FName: string; end;
补充说明
如果你的Attribute类需要接收参数,才需要在标注时添加括号并传入对应参数,示例如下:
Field = class(TCustomAttribute) public constructor Create(const AFieldName: string); end; TUser = class private [Field('UserName')] // 带参数时需保留括号并传入值 FName: string; end;
内容的提问来源于stack exchange,提问作者haydon
相关产品推荐
相关产品推荐

