[英]How to set default value in Anonymous Class if any property is null;
I have a class as following.我有一个课程如下。
public class CommonResponse<T>
{
public HttpStatusCode ResponseCode { get; set; }
public string ResponseMessage { get; set; }
public T data { get; set; }
}
there are the following option.有以下选项。
//1.
CommonResponse<string> obj1=new CommonResponse<string>(){ResponseCode=200, ResponseMessage="OK", data=null};
//2.
CommonResponse<string> obj2=new CommonResponse<class1>(){ResponseCode=200, ResponseMessage="OK", data=new class1{p1="", p2=null, p3=null}};
//3.
CommonResponse<string> obj3=new CommonResponse<class2>(){ResponseCode=200, ResponseMessage="OK", data=null};
i am looking a code that convert every null value into default value.我正在寻找将每个空值转换为默认值的代码。 the default values are as following
默认值如下
1 type(string)=string.empty; 1 类型(字符串)=字符串.空;
2 type(int)=0; 2 类型(整数)=0;
etc.等等。
i am trying the following code.我正在尝试以下代码。 but it is not working.
但它不起作用。
public class CommonResponse<T>
{
private T _data { get; set; }
public HttpStatusCode ResponseCode { get; set; }
public string ResponseMessage { get; set; }
public T data
{
get
{
if (_data != null)
{
PropertyInfo[] properties =
_data.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var item in properties)
{
if (item.PropertyType == typeof(string))
{
var value = item.GetValue(_data, null);
if (value == null)
{
item.SetValue(_data, Convert.ChangeType(string.Empty, item.PropertyType), null);
}
}
if (item.PropertyType == typeof(int))
{
var value = item.GetValue(_data, null);
if (value == null)
{
item.SetValue(_data, Convert.ChangeType(0, item.PropertyType), null);
}
}
}
return _data;
}
else
{
return (T)Convert.ChangeType(string.Empty, typeof(T));
}
}
set
{
_data = value;
}
}
}
i need the following output我需要以下输出
//1.
obj1={ResponseCode=200, ResponseMessage="OK", data=""};
//2.
obj2={ResponseCode=200, ResponseMessage="OK", data=new class1{p1="", p2="", p3=0}};
//3.
obj3={ResponseCode=200, ResponseMessage="OK", data=""};
for example例如
public class class1
{
public string p1{get;set;}
public string p1{get;set;}
public int? p3{get;set;}
}
CommonResponse<string> obj2=new CommonResponse<class1>(){ResponseCode=200, ResponseMessage="OK", data=new class1{p1="hello", p2=null, p3=null}};
var json = new JavaScriptSerializer().Serialize(obj2);
Console.WriteLine(json);
the output is as following输出如下
{"ResponseCode":"200", "ResponseMessage":"OK", data:{"p1":"hello", "p2":null, "p3": null}} {"ResponseCode":"200", "ResponseMessage":"OK", data:{"p1":"hello", "p2":null, "p3": null}}
There are two null fields p2 and p3有两个空字段 p2 和 p3
I need that every null value convert into empty string.我需要将每个空值都转换为空字符串。 the desired output is as following.
所需的输出如下。
{"ResponseCode":"200", "ResponseMessage":"OK", data:{"p1":"hello", "p2":"", "p3": "0"}} {"ResponseCode":"200", "ResponseMessage":"OK", data:{"p1":"hello", "p2":"", "p3": "0"}}
It seems you are not using set
properly, so data is always null.看来您没有正确使用
set
,因此数据始终为空。
You should change你应该改变
set
{
_data = data;
}
to:到:
set
{
_data = value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.