gpt4 book ai didi

delphi - 错误的 RTTI 可见性信息和缺少的属性

转载 作者:行者123 更新时间:2023-12-01 15:20:14 25 4
gpt4 key购买 nike

我的应用程序中本质上有以下类(class)

TCategory = class(TAbstractionTreeItem)
private
fName: String;
fParent: Integer;
fComment: String;
public
procedure Default; override;
protected
procedure Validate(Validation: TValidation); override;
published
[AbstractionField]
property Name: string read fName write fName;
[AbstractionField]
property Parent: Integer read fParent write fParent;
[AbstractionField]
property Comment: String read fComment write fComment;
end;

当我现在尝试通过 Delphi XE 中的高级 RTTI 获取信息时,我得到了已发布属性的可见性信息,结果告诉我它们只是公共(public)的,而我添加的属性根本没有显示。

那里发生了什么?我已经尝试验证它是我尝试分析的正确类,并且在发生更改时重新编译属于它的单元。这似乎不是问题。

最佳答案

为了让您提供的代码进行编译,我更改了以下内容:

AbstractionField = class(TCustomAttribute)
end;

TCategory = class(TObject)
private
fName: String;
fParent: Integer;
fComment: String;
public
procedure Default;
protected
procedure Validate(Validation: Integer);
published
[AbstractionField]
property Name: string read fName write fName;
[AbstractionField]
property Parent: Integer read fParent write fParent;
[AbstractionField]
property Comment: String read fComment write fComment;
end;

然后我编写了以下代码来查询属性的可见性:
var
C : TRttiContext;
T : TRttiType;
P : TRttiProperty;

begin
T := C.GetType(TCategory.ClassInfo);
for P in T.GetProperties do
begin
Memo1.Lines.Add(P.Name + ' ' +
GetEnumName(TypeInfo(TMemberVisibility),ord(P.Visibility)) );
end;
end;

我的结果(如预期):
Name mvPublished
Parent mvPublished
Comment mvPublished

我也在使用 Delphi XE,您将不得不提供更多代码,以便我们可以复制问题。

还要确保检查警告:
[DCC 警告] UnitName.pas(LineNum):W1025 不支持的语言功能:“自定义属性”

这是识别属性是否输入错误且编译器无法找到的唯一方法。

关于delphi - 错误的 RTTI 可见性信息和缺少的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5778180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com