【问题标题】:How to set default value in Anonymous Class if any property is null;如果任何属性为空,如何在匿名类中设置默认值;
【发布时间】:2020-02-11 13:27:30
【问题描述】:

我有一门课如下。

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 type(string)=string.empty;

2 类型(int)=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", 数据:{"p1":"hello", "p2":"", "p3": "0"}}

【问题讨论】:

  • 什么不完全有效?
  • 使用 getter setter 后,即使数据有值,每次都会返回 string.empty。

标签: c# anonymous


【解决方案1】:

您似乎没有正确使用set,因此数据始终为空。

你应该改变

set
{
    _data = data;
}

到:

set
{
    _data = value;
}

【讨论】:

  • 没关系,但我仍然无法设置默认值。
  • 请详细说明您无法转换默认值的哪些属性和类型?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-20
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 2013-05-21
  • 2010-11-18
相关资源
最近更新 更多