【问题标题】:Wrong RTTI Visibility Information and missing attributes错误的 RTTI 可见性信息和缺少的属性
【发布时间】:2011-04-25 12:11:22
【问题描述】:

我的应用程序中有以下类

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

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

【问题讨论】:

    标签: delphi attributes visibility delphi-xe rtti


    【解决方案1】:

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

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

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

    【讨论】:

    • 是的,这有点奇怪,我只是尝试在具有完全相同的类的不同控制台应用程序中重现它,并且它工作正常,然后切换回我的原始项目并清理 dcu第三次文件它现在也可以在那里工作,非常神秘。也许编译器有问题,我不知道。但是为了感谢您的努力,以及有关属性的宝贵提示,我会接受这个答案。
    • 在我的情况下,编译器似乎不时会因为某些未知原因而感到困惑。似乎有帮助的是通过将{$RTTI INHERIT PROPERTIES([vcPublic, vcPublished])} 放在 TCategory 的基类上来友好地提醒他他的职责。到目前为止,我无法将问题简化为简单的比例。它只是在我具体的整个项目中失败了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    相关资源
    最近更新 更多