2011-04-25 92 views
1

我在本質上是下面的類在我的應用錯誤RTTI可見性信息和缺少的屬性

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; 

當我現在嘗試通過先進的RTTI在Delphi XE獲取有關信息,我得到作爲發佈的可見性信息屬性的結果,告訴我他們只是公開的,我添加的屬性根本沒有顯示出來。

這是怎麼回事?我已經嘗試驗證它是我嘗試分析的正確類,並且在發生更改時重新編譯屬於它的單元。這似乎不是問題。

回答

5

,以獲得提供的編譯代碼,我改變了以下內容:

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以及您將不得不提供mor e代碼,以便我們可以複製該問題。

還要確保您檢查您的警告: [DCC警告] UnitName.pas(LINENUM):W1025不支持的語言特性:「自定義屬性」

這是識別屬性是否正確的唯一途徑輸入並且不能被編譯器找到。

+1

是的,這有點奇怪,我只是嘗試在不同的控制檯應用程序中使用完全相同的類重現它,並且它工作正常,然後切換回我的原始項目並第三次清理dcu文件它現在也在那裏工作,非常神祕。也許編譯器有一個hickup,我不知道。但爲了感謝你的努力,以及有價值的特性,我會接受這個答案。 – HalloDu 2011-04-25 15:38:58

+1

看來在我的情況下,編譯器有時會因爲某些未知的原因而感到困惑。看起來有所幫助的是通過在TCategory的基類上放置'{$ RTTI INHERIT PROPERTIES([vcPublic,vcPublished])}'來友好地提醒他自己的責任。直到現在,我還是無法將問題簡化爲簡單的比例。這只是我的具體整個項目失敗。 – HalloDu 2011-04-27 20:05:59