Rtti 不适用于用作类字段的泛型类型
我在使用 rtti 获取有关泛型类型的类字段的信息时遇到问题。经过一番谷歌搜索后,我在 QC 中找到了一个条目,描述了的问题。我的问题是,是否有人知道解决方法,或者是否已修复 Delphi XE2。以下是 QC 重现该错误的源代码片段。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,这个错误在 Delphi XE2 中仍然存在,作为解决方法,您可以像这样声明
TIntList
类型Unfortunally this bug is still present in Delphi XE2, as Workaround you can declare the
TIntList
type like this