2017-01-29 38 views
0

我正在實現一個包,用於在delphi IDE中轉換和自動生成組件。我知道GExperts具有類似的功能,但我需要定製一些特定的屬性。Delphi OpenTools API獲取組件屬性

現在我卡在訪問TADOQuery.SQL屬性,它是字符串列表一個實例:我真的不知道是否使用TValue從RTTI是正確的方式來

var 
    aVal : TValue; 
    aSqlS : TStrings; 
begin 
    [...] 
    if (mycomp.GetComponentType = 'TADOQuery') then 
     if mycomp.GetPropValueByName('SQL', aVal) then 
     begin 
      aSqlS := TStrings(aVal.AsClass); 
      if Assigned(aSqlS) then    <----- problem is here 
       ShowMessage(aSqlS.Text);  <----- problem is here 
     end; 
end; 

走。

感謝

+1

IIRC,'aVal'應該是'IOTAComponent'或'TIComponentInterface'類型。它取決於'mycomp'的類型。 –

回答

2

假設GetPropValueByName()返回一個有效TValue(你沒有顯示的代碼),然後使用aVal.AsClass是錯誤的,因爲SQL屬性getter不返回元類類型。它返回一個對象指針,所以用aVal.AsObject來代替,或者甚至用aVal.AsType<TStrings>


更新如果comp實際上是IOTAComponentTValue肯定是不對的在所有使用。的IOTAComponent.GetPropValueByName()輸出是一個無類型var接收屬性值的原始數據,或用於IOTAComponent衍生TPersistent對象:

var 
    aVal: IOTAComponent; 
    aSqlS : TStrings; 
begin 
    [...] 
    if (mycomp.GetComponentType = 'TADOQuery') then 
     if mycomp.PropValueByName('SQL', aVal) then 
     ShowMessage(TStrings(aVal.GetComponentHandle).Text); 
end; 

然而,更好的辦法是訪問實際TADOQuery對象,而不是:

if (mycomp.GetComponentType = 'TADOQuery') then 
    ShowMessage(TADOQuery(comp.GetComponentHandle).SQL.Text);