加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 为什么我的代码不能编译,而是获取E2506接口部分声明的

发布时间:2020-12-15 09:28:00 所属栏目:大数据 来源:网络整理
导读:我正在使用Delphi XE. 以下单元无法使用此错误进行编译: [DCC Error] GTSJSONSerializer.pas(27): E2506 Method of parameterized type declared in interface section must not use local symbol 'TSuperRttiContext.AsJsonGTSJSONSerializer.TGTSJSONSeri
我正在使用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有关吗?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读