gpt4 book ai didi

delphi - 为什么我的代码无法编译,而是得到 E2506 在接口(interface)部分声明的参数化类型的方法不能使用本地符号

转载 作者:行者123 更新时间:2023-12-03 15:11:59 25 4
gpt4 key购买 nike

我正在使用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.

最佳答案

来自XE2 DocWiki :

This happens when trying to assign a literal value to a generics data field.

program E2506;

{$APPTYPE CONSOLE}

uses
SysUtils;

type
TRec<T> = record
public
class var x: Integer;
class constructor Create;
end;

class constructor TRec<T>.Create;
begin
x := 4; // <-- e2506 Fix: overload the Create method to
// take one parameter x and assign it to the x field.
end;

begin
Writeln('E2506 Method of parameterized type declared' +
' in interface section must not use local symbol');
end.

不过,我无法判断它可能反对哪个局部变量;您在 SerialObjectToJSON 中有一个本地文件,在 DeserializeJSONToObject 中有两个本地文件。根据链接的修复,我也不确定它如何适用于您发布的代码。会不会和TSuperRTTIContext有关?

关于delphi - 为什么我的代码无法编译,而是得到 E2506 在接口(interface)部分声明的参数化类型的方法不能使用本地符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8363146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com