首页 > 解决方案 > c#没有为DTO属性中的类型定义无参数构造函数

问题描述

我有一个问题,我有一个使用 RestClient 获取信息的简单操作,代码如下所示:

 var client = new RestClient(RestClientBaseUrl);
 IRestResponse request = client.Execute(new RestRequest("Application/GetApplications", Method.GET));
 IEnumerable<DTO.Application> response = new JsonDeserializer().Deserialize<IEnumerable<DTO.Application>>(request);

此模型的 DTO 如下所示:

public class Application
{
    public int? Id { get; set; }
    public string Name { get; set; }
    public GetSystem[] Systems { get; set; }
}

对于系统,它看起来像:

public class GetSystem : SystemTemplate
{
    public int Id { get; set; }
    public SystemState SystemState { get; set; }
}

系统模板

public abstract class SystemTemplate
{
    public string Name { get; set; }
    public string IpAddress { get; set; }
    public int Port { get; set; }
}

和系统状态:

public class SystemState
{
    public int? Id { get; set; }
    public byte MaintenanceMode { get; set; }
    public string LastCommunicationTimestamp { get; set; }
    public int SystemId { get; set; }
}

现在的问题是当我反序列化 Json 时,我得到:

System.MissingMethodException : No parameterless constructor defined for type 'DS.Models.DTO.GetSystem[]'.

可能是什么问题呢?

标签: c#json

解决方案


您需要在 GetSystem 类中添加一个空构造函数。


推荐阅读