2011-12-06 57 views
7

我在使用rtti獲取有關泛型類型的類字段的信息時遇到了問題。經過相當一些googleing後,我在QC找到了一個描述該問題的條目。我的問題是,如果有人知道解決方法,或者這是固定的Delphi XE2。以下是來自QC的源代碼片段,用於重現該錯誤。Rtti不適用於用作類字段的泛型類型

program Generics; 

    {$APPTYPE CONSOLE} 

    uses 
     Generics.Collections, Rtti, SysUtils; 

    type 
     TIntList = TList<Integer>; 

     TRecContainer = record 
     FList: TIntList; 
     end; 

     TObjContainer = class 
     FList: TIntList; 
     end; 

    var 
     ctx: TRttiContext; 
     f: TRttiField; 

    begin 
     ctx := TRttiContext.Create; 
     try 
     for f in ctx.GetType(TypeInfo(TRecContainer)).GetFields do 
      if f.FieldType <> nil then 
      writeln(f.FieldType.Name) 
      else 
      writeln('f.FieldType = nil'); 
     for f in ctx.GetType(TypeInfo(TObjContainer)).GetFields do 
      if f.FieldType <> nil then 
      writeln(f.FieldType.Name) 
      else 
      writeln('f.FieldType = nil'); 
     finally 
     ctx.Free; 
     readln; 
     end; 
    end. 
+0

好了,我可以證實它不是在XE2工作。 –

回答

8

Unfortunally這個錯誤仍然存​​在於德爾福XE2,作爲解決方法你可以聲明TIntList型這樣

TIntList = class(TList<Integer>); 
+0

謝謝,解決了我的問題(使用[Collections](http://code.google.com/p/delphi-coll/)進行序列化) – iamjoosy