2009-10-12 2 views
5

는 다음과 같은 고려 : TMainClass.GetValue에서RTTI를 사용하여 Delphi에서 액세스 필드를 얻는 방법은 무엇입니까?

TFieldType = class 
    fValue: string; 
end; 

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

I 해요 노력 얻을 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

당신은 RTTI의 더 나은 이해를 위해

vField.GetValue(self);

처럼 GetValue의 매개 변수로 인스턴스를 통과해야 로버트 사랑하여 remarkable articles about RTTI를 참조하십시오. 이 문제에 대해서는 Properties and Fields에 대한이 specialy this.

+0

고마워, 나는이 기사를 읽었지만 그렇게 열심히하지 않는 것 같다. 귀하의 대답은 내 문제를 해결합니다. – boombastic

관련 문제