错误的 RTTI 可见性信息和缺失的属性

发布于 2024-11-03 02:47:04 字数 688 浏览 2 评论 0原文

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

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 获取有关信息时,我得到的结果是已发布属性的可见性信息,该结果告诉我它们只是公共的,而我添加的属性是根本没有出现。

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

I have in essence the following class in my application

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;

When I now try to get information about through advanced RTTI in Delphi XE, I get as visibility information for the published properties a result that tells me they are only public and the attributes that I added are not showing up at all.

What is going on there? I already tried to validate that it is the right class that I try to analyse and that the unit that belongs to it is recompiled, when changes occur. That does not seem to be the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2024-11-10 02:47:04

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

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 不支持的语言功能:“自定义属性”

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

In order to get your code provided to compile, i changed the following:

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;

Then I wrote the following code to query the visibility of the properties:

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;

My Results where (As Expected):

Name mvPublished
Parent mvPublished
Comment mvPublished

I am using Delphi XE as well, your going to have to provide more code so we can duplicate the problem.

Also make sure you check your warnings for:
[DCC Warning] UnitName.pas(LineNum): W1025 Unsupported language feature: 'custom attribute'

This is the only way to identify if an attribute is incorrectly entered and unable to be found by the compiler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文