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

发布于 2024-12-19 04:48:39 字数 1183 浏览 3 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你只要分分秒秒 2024-12-26 04:48:39

来自 XE2 DocWiki

当尝试将文字值分配给泛型数据字段时,会发生这种情况。

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有关?

From the 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.

I can't tell which of the local variables it might be objecting to, though; you have one local in SerialObjectToJSON and two in DeserializeJSONToObject. I'm also not sure based on the linked fix exactly how that applies to the code you posted. Could it be related to TSuperRTTIContext?

薄凉少年不暖心 2024-12-26 04:48:39

我可以使用 D2010、DXE 和 DXE2 针对 SuperObject 修订版 46 来编译您的设备。

I can compile your unit with D2010, DXE and DXE2 against SuperObject revision 46.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文