Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从具有null属性的基类初始化_C# - Fatal编程技术网

C# 从具有null属性的基类初始化

C# 从具有null属性的基类初始化,c#,C#,我有以下界面: public interface IResponse<T> { IList<Error> Errors { get; } IPaging Paging { get; } IList<T> Result { get; } } 就我所拥有的而言,这是行不通的(我认为,在这种情况下,没有模型): Response-Response=新响应(); 这样做的原因是,我将把响应转换为JSON,并且我仍然希望显示分页和结果 最好的方法

我有以下界面:

public interface IResponse<T> {
  IList<Error> Errors { get; }
  IPaging Paging { get; }
  IList<T> Result { get; }    
}
就我所拥有的而言,这是行不通的(我认为,在这种情况下,没有模型):

Response-Response=新响应();
这样做的原因是,我将把响应转换为JSON,并且我仍然希望显示分页和结果


最好的方法是什么?

您不需要对模型进行任何代码更改。如果您使用Newtonsoft进行序列化,可以执行以下操作:

void Main() {
    Response<MyResponse> myResponse = new Response<MyResponse>(new List<MyResponse>(), null, null);
    var serializer = new JsonSerializer();
    StringBuilder sb = new StringBuilder();

    using(var writer = new StringWriter(sb)) {
        using (var jWriter = new JsonTextWriter(writer)) {
            serializer.NullValueHandling = NullValueHandling.Ignore;
            serializer.Serialize(jWriter, myResponse);
        }
    }

    Console.WriteLine(sb.ToString());
}

public interface IResponse<T> {
  IList<Error> Errors { get; }
  IPaging Paging { get; }
  IList<T> Result { get; }    
}

public class Response<T> : IResponse<T> {

  public IList<Error> Errors { get; private set; }
  public IPaging Paging { get; private set; }
  public IList<T> Result { get; private set; }

  public Response(IList<T> result, IPaging paging, IList<Error> errors) {
    Errors = errors;
    Paging = paging;
    Result = result;
  }

}

public class Error {


}

public interface IPaging {

}

public class MyResponse {
    public string Name {get; set;}
}
通过更改此行上的序列化程序设置:

serializer.NullValueHandling = NullValueHandling.Ignore;

结果是:

{"Errors":null,"Paging":null,"Result":[]}

新响应(空、空、空)
?或者提供空列表和实现
IPaging
的“空对象”。还不完全清楚“它们为null但仍存在于对象中”是什么意思。“它们为null但仍存在于类中”。。。因此,当我转换为Json时,分页和结果仍然显示为属性,但为null。如果属性没有显示在输出中,您可能需要调整Json序列化设置,使其始终包含null。假设您使用的是Newtonsoft:最大的问题是如何将响应序列化为json。
Response<?> response = new Response<?>();
void Main() {
    Response<MyResponse> myResponse = new Response<MyResponse>(new List<MyResponse>(), null, null);
    var serializer = new JsonSerializer();
    StringBuilder sb = new StringBuilder();

    using(var writer = new StringWriter(sb)) {
        using (var jWriter = new JsonTextWriter(writer)) {
            serializer.NullValueHandling = NullValueHandling.Ignore;
            serializer.Serialize(jWriter, myResponse);
        }
    }

    Console.WriteLine(sb.ToString());
}

public interface IResponse<T> {
  IList<Error> Errors { get; }
  IPaging Paging { get; }
  IList<T> Result { get; }    
}

public class Response<T> : IResponse<T> {

  public IList<Error> Errors { get; private set; }
  public IPaging Paging { get; private set; }
  public IList<T> Result { get; private set; }

  public Response(IList<T> result, IPaging paging, IList<Error> errors) {
    Errors = errors;
    Paging = paging;
    Result = result;
  }

}

public class Error {


}

public interface IPaging {

}

public class MyResponse {
    public string Name {get; set;}
}
{"Result":[]}
serializer.NullValueHandling = NullValueHandling.Ignore;
serializer.NullValueHandling = NullValueHandling.Include;
{"Errors":null,"Paging":null,"Result":[]}