为什么我的代码无法编译,而是得到 E2506 在接口部分声明的参数化类型的方法不能使用本地符号
我正在使用Delphi XE。
以下单元无法编译并出现此错误:
[DCC Error] GTSJSONSerializer.pas(27): E2506 Method of parameterized type declared
in interface section must not use
local symbol 'TSuperRttiContext.AsJson<GTSJSONSerializer.TGTSJSONSerializer<T>.T>'
这是为什么?有解决方法吗?
unit GTSJSONSerializer;
interface
type
TGTSJSONSerializer<T> = class
class function SerializeObjectToJSON(const aObject: T): string;
class function DeserializeJSONToObject(const aJSON: string): T;
end;
implementation
uses
SuperObject
;
class function TGTSJSONSerializer<T>.SerializeObjectToJSON(const aObject: T): string;
var
SRC: TSuperRttiContext;
begin
SRC := TSuperRttiContext.Create;
try
Result := SRC.AsJson<T>(aObject).AsString;
finally
SRC.Free;
end;
end;
class function TGTSJSONSerializer<T>.DeserializeJSONToObject(const aJSON: string): T;
var
LocalSO: ISuperObject;
SRC: TSuperRttiContext;
begin
SRC := TSuperRttiContext.Create;
try
LocalSO := SO(aJSON);
Result := SRC.AsType<T>(LocalSO);
finally
SRC.Free;
end;
end;
end.
I am using Delphi XE.
The following unit fails to compile with this error:
[DCC Error] GTSJSONSerializer.pas(27): E2506 Method of parameterized type declared
in interface section must not use
local symbol 'TSuperRttiContext.AsJson<GTSJSONSerializer.TGTSJSONSerializer<T>.T>'
Why is that? Is there a workaround?
unit GTSJSONSerializer;
interface
type
TGTSJSONSerializer<T> = class
class function SerializeObjectToJSON(const aObject: T): string;
class function DeserializeJSONToObject(const aJSON: string): T;
end;
implementation
uses
SuperObject
;
class function TGTSJSONSerializer<T>.SerializeObjectToJSON(const aObject: T): string;
var
SRC: TSuperRttiContext;
begin
SRC := TSuperRttiContext.Create;
try
Result := SRC.AsJson<T>(aObject).AsString;
finally
SRC.Free;
end;
end;
class function TGTSJSONSerializer<T>.DeserializeJSONToObject(const aJSON: string): T;
var
LocalSO: ISuperObject;
SRC: TSuperRttiContext;
begin
SRC := TSuperRttiContext.Create;
try
LocalSO := SO(aJSON);
Result := SRC.AsType<T>(LocalSO);
finally
SRC.Free;
end;
end;
end.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 XE2 DocWiki:
不过,我无法判断它可能会反对哪些局部变量;您在
SerialObjectToJSON
中有一个本地文件,在DeserializeJSONToObject
中有两个本地文件。根据链接的修复,我也不确定它如何适用于您发布的代码。会不会和TSuperRTTIContext
有关?From the XE2 DocWiki:
I can't tell which of the local variables it might be objecting to, though; you have one local in
SerialObjectToJSON
and two inDeserializeJSONToObject
. I'm also not sure based on the linked fix exactly how that applies to the code you posted. Could it be related toTSuperRTTIContext
?我可以使用 D2010、DXE 和 DXE2 针对 SuperObject 修订版 46 来编译您的设备。
I can compile your unit with D2010, DXE and DXE2 against SuperObject revision 46.