2009-10-12 37 views
5

考慮以下幾點:如何使用RTTI在Delphi中獲取訪問字段?

TFieldType = class 
    fValue: string; 
end; 

TMainClass = class 
private 
    Ffield: TFieldType; 
public 
    function GetValue: string; 
end; 

在TMainClass.GetValue我試着讓TMainClass字段的值:

function TMainClass.GetValue; 
begin 
    vCtx := TRTTIContext.Create; 
    vType := vCtx.GetType(Self.ClassInfo); 
    for vField in vType.GetFields do 
    vField.GetValue(
     //Here's the trouble, because i don't know how to get the instance 
    ); 

可能有獲得它們的實例字段的值的另一種方式另一類?

回答

6

你必須通過實例作爲的GetValue的參數一樣

vField.GetValue(self);

爲了更好地理解RTTI的羅伯特·愛讀remarkable articles about RTTI。對於這個問題specialy這個約Properties and Fields

+0

非常感謝,我閱讀過這些文章,但看起來並不那麼專心。你的回答解決了我的問題。 – boombastic 2009-10-12 08:50:28