Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rtti does not work with generics types used as class fields

I had problems using rtti to get information about class fields of a generic type. After quite some googleing I found an entry in QC describing the issue. My question is, if anybody knows a workaround, or if this got fixed Delphi XE2. Below is the source snippet from QC to reproduce the bug.

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.
like image 819
iamjoosy Avatar asked Dec 06 '11 18:12

iamjoosy


1 Answers

Unfortunally this bug is still present in Delphi XE2, as Workaround you can declare the TIntList type like this

TIntList = class(TList<Integer>);
like image 186
RRUZ Avatar answered Sep 23 '22 18:09

RRUZ