2011-05-15 3 views
3

간단한 샘플을 확인하십시오 (실제 시나리오가 다릅니다). 개체의 중첩 된 속성 값을 설정하고 싶습니다.이 경우에는 TLabel 구성 요소의 글꼴은 RTTI를 사용하여 clRed입니다. RTTI를 사용하여 중첩 된 속성의 값을 설정할 수있는 방법

var 
    p : TRttiProperty; 
    p2: TRttiProperty; 
    c : TRttiContext; 
begin 
    c := TRttiContext.Create; 
    try 
    p := c.GetType(Label1.ClassInfo).GetProperty('Font'); 
    p2 := c.GetType(p.PropertyType.Handle).GetProperty('Color'); 
    p2.SetValue(p.PropertyType.AsInstance,clred); //this line is not working 
    finally 
    c.Free; 
    end; 
end; 

은 내가

p2.SetValue(Label1,clred); 

답변

3

다음 코드는 작동을 시도했다.

var 
    p : TRttiProperty; 
    p2: TRttiProperty; 
    c : TRttiContext; 
begin 
    c := TRttiContext.Create; 
    try 
    p := c.GetType(Label1.ClassInfo).GetProperty('Font'); 
    p2 := c.GetType(p.PropertyType.Handle).GetProperty('Color'); 
    p2.SetValue(p.GetValue(Label1).AsObject,clred); //this line now works. 
    finally 
    c.Free; 
    end; 
end; 

레이블에서 임베디드 글꼴을 가져와야합니다. TRttiProperty는 인스턴스가 아닌 유형을 처리합니다. 인스턴스를 처리하려면 GetValue() 또는 SetValue()으로 전화해야합니다.

원본 코드가 인스턴스가 아닌 유형을 참조하고 있습니다.

+0

대단히 감사합니다. Robert. – Salvador

관련 문제