简体   繁体   English

如果任何属性为空,如何在匿名类中设置默认值;

[英]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.

相关问题 如何为匿名对象的属性设置值? - How to set value for property of an anonymous object? LINQ:如果导航属性为null,则设置默认值 - LINQ: Set a default value if navigation property is null C#:如何为部分类中的属性设置默认值? - C#: How to set default value for a property in a partial class? 如何在类属性的模拟单元测试中设置默认值? - How to set default value inside mock unit test for class property? 在 class 的匿名属性中设置 DeserializeObject - Set DeserializeObject in anonymous property of a class 如何避免for循环和null比较将属性的初始值设置为默认值 - How to avoid for loop and null comparison to set initial value of a property to a default value 是否可以基于另一个属性将set属性的默认值分配给set? - Is it possible to assign set the default value of a class property based on another property? 使用自定义 ContractResolver,在将 null JSON 属性反序列化为值类型成员时如何设置默认值而不是 null? - Using a custom ContractResolver, how to set a default value instead of null when deserializing a null JSON property to a value-type member? 如何将类中的默认值设置为另一个类的属性? - How can I set a default value inside of a class as a property of another class? 如何声明匿名类的属性 - How to declare a property of an anonymous class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM