Delphi泛型EConversionError不能实例化类型

我想从JSON字符串响应中将数据反序列化到Delphi类中 - 有没有一种方法可以在Delphi中使用泛型来实现,所以我可以为每种不同类型的响应定义一个POCO类 - 在默认的Data属性中响应班? 我在C#中有更好的知识,并使用JavaScriptSerializer反序列化方法完成它,如下所示:

public Response<T> Get<T>(string methodUrl) where T: class
...
 var _json_serializer = new JavaScriptSerializer();
 _response = _json_serializer.Deserialize<Response<T>>(api_response);
...

public class Response
{
    public bool Success { get; set; }
    public string ErrorMessage { get; set; }
}    

public class Response<T> : Response
{
    public T Data;
    public Response()
    {
        Data = default(T);
        Success = false;
        ErrorMessage = Settings.Default.errString;
    }
}

我可以这样称呼它:

response = _webapi.Get<GetTestClass>(url_userinfo + sUserName);

internal class GetTestClass
{
    public string DomainName { get; set; }
    public int Code { get; set; }
}

德尔福有没有办法做到这一点? 我使用函数GetUrlContent从Web API服务实现了Delphi10.1中的GET方法 - 我得到了有效的字符串响应。 所以我尝试了这个,它编译了,但是当它试图填充这个类时我得到了一个错误:

uses PPUserInfoDetails, APIRootClass;
var answer_class: TRootClass<TPPUserInfoDetails>;
    Test_APIGet: string;
...
Test_APIGet := GetUrlContent(url_userinfo + sUserName);    
answer_class:= TRootClass<TPPUserInfoDetails>.FromJsonString(Test_APIGet);
...
unit PPUserInfoDetails;
interface
type

TDataClass = class
private
  FDisplayName: String;
  FCode: Integer;
  FRights: TArray<Integer>;
public
  property DisplayName: String read FDisplayName write FDisplayName;
  property Code: Integer read FCode write FCode;
  property Rights: TArray<Integer> read FRights write FRights;
end;
implementation
end.

unit APIRootClass;
interface
uses Rest.Json , sysutils;
type

TRootClass<T> = class
private
  FData: T;
  FErrorMessage: String;
  FSuccess: Boolean;
public
  property Data: T read FData write FData;
  property ErrorMessage: String read FErrorMessage write FErrorMessage;
  property Success: Boolean read FSuccess write FSuccess;
  constructor Create;
  destructor Destroy; override;
  function ToJsonString: string;
  class function FromJsonString(AJsonString: string): TRootClass<T>;
end;

implementation

constructor TRootClass<T>.Create;
begin
  inherited;
  FData := Default(T);
end;

destructor TRootClass<T>.Destroy;
begin
  freeandnil(FData);
  inherited;
end;

function TRootClass<T>.ToJsonString: string;
begin
  result := TJson.ObjectToJsonString(self);
end;

class function TRootClass<T>.FromJsonString(AJsonString: string): TRootClass<T>;
begin
  result := TJson.JsonToObject<TRootClass<T>>(AJsonString)
end;

我得到错误:项目Test.exe引发异常类EConversionError消息'内部:无法实例化类型APIRootClass.TRootClass'

当我在我的APINoGenerics单元中定义没有类似泛型的类时,我可以将数据反序列化为特定的格式类:

uses APINoGenerics;
var answer_class: TRootClass;
...
Test_APIGet := GetUrlContent(url_userinfo + sUserName);
answer_class := TRootClass.FromJsonString(Test_APIGet);
...
unit APINoGenerics;
interface
uses Rest.Json;
type

TDataClass = class
private
  FDisplayName: String;
  FCode: Integer;
  FRights: TArray<Integer>;
public
  property DisplayName: String read FDisplayName write FDisplayName;
  property Code: Integer read FCode write FCode;
  property Rights: TArray<Integer> read FRights write FRights;
end;

TRootClass = class
private
  FData: TDataClass;
  FErrorMessage: String;
  FSuccess: Boolean;
public
  property Data: TDataClass read FData write FData;
  property ErrorMessage: String read FErrorMessage write FErrorMessage;
  property Success: Boolean read FSuccess write FSuccess;
  constructor Create;
  destructor Destroy; override;
  function ToJsonString: string;
  class function FromJsonString(AJsonString: string): TRootClass;
end;

implementation

constructor TRootClass.Create;
begin
  inherited;
  FData := TDataClass.Create();
end;

destructor TRootClass.Destroy;
begin
  FData.free;
  inherited;
end;

function TRootClass.ToJsonString: string;
begin
  result := TJson.ObjectToJsonString(self);
end;

class function TRootClass.FromJsonString(AJsonString: string): TRootClass;
begin
  result := TJson.JsonToObject<TRootClass>(AJsonString)
end;

我希望能够在Delphi中使用泛型 - 所以我只想知道它是否可行以及如何? 我试图寻找答案,但主要是遇到泛型不能在Delphi中运行的答案,所以我发布了这个答案,因为我没有看到这种类型的问题。

链接地址: http://www.djcxy.com/p/91107.html

上一篇: Delphi generics EConversionError cannot instantiate type

下一篇: Delphi F2084 Internal Error: AV07953449