簡體   English   中英

如果任何屬性為空,如何在匿名類中設置默認值;

[英]How to set default value in Anonymous Class if any property is null;

我有一個課程如下。

public class CommonResponse<T>
{
    public HttpStatusCode ResponseCode { get; set; }
    public string ResponseMessage { get; set; }
    public T data { get; set; }
}

有以下選項。

//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};

我正在尋找將每個空值轉換為默認值的代碼。 默認值如下

1 類型(字符串)=字符串.空;

2 類型(整數)=0;

等等。

我正在嘗試以下代碼。 但它不起作用。

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;
        }
    }
}

我需要以下輸出

//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=""};

例如

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);

輸出如下

{"ResponseCode":"200", "ResponseMessage":"OK", data:{"p1":"hello", "p2":null, "p3": null}}

有兩個空字段 p2 和 p3

我需要將每個空值都轉換為空字符串。 所需的輸出如下。

{"ResponseCode":"200", "ResponseMessage":"OK", data:{"p1":"hello", "p2":"", "p3": "0"}}

看來您沒有正確使用set ,因此數據始終為空。

你應該改變

set
{
    _data = data;
}

到:

set
{
    _data = value;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM