我想要使用带有RTTI的通用记录,但在类型信息方面遇到了问题。有没有人知道为什么以下代码无法在Delphi 2010中编译?
program GenericTypeInfo;
{$APPTYPE CONSOLE}
uses
TypInfo,
SysUtils;
type
TMyRec<T> = record
public
Value: T;
end;
TMyInt = TMyRec<Integer>;
TMyString = TMyRec<String>;
begin
try
Writeln(GetTypeName(TypeInfo(TMyRec<Integer>))); <--- This works fine
Writeln(GetTypeName(TypeInfo(TMyRec<String>))); <--- so does this
Writeln(GetTypeName(TypeInfo(TMyInt))); <--- BUT this won't compile
Writeln(GetTypeName(TypeInfo(TMyString))); <--- nor this!!
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
上面标示的这些行会产生以下编译器错误:
[DCC Error] GenericTypeInfo.dpr(24): E2134 Type 'TMyInt' has no type info
[DCC Error] GenericTypeInfo.dpr(24): E2134 Type 'TMyString' has no type info
我不明白这两者之间有什么重大区别?虽然我不是一个低级专家,但编译器为什么要对此进行不同处理?我需要它适用于TMyInt和TMyString类型。
感谢任何帮助。