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.
Unfortunally this bug is still present in Delphi XE2, as Workaround you can declare the TIntList
type like this
TIntList = class(TList<Integer>);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With