Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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# 从对象属性中删除空值_C#_Object_Properties_Null - Fatal编程技术网

C# 从对象属性中删除空值

C# 从对象属性中删除空值,c#,object,properties,null,C#,Object,Properties,Null,我正在寻找一种将“null”属性值设置为“non-null”值的方法。这些特性与一个对象关联,并且存在多个对象的列表 我遇到的问题是将“null”值转换为“non-null”值,其中每个属性都有不同的类型 到目前为止,我有一些嵌套的循环和条件,试图识别null属性并将它们设置为非null //loop through each object for (int i = 0; i < objectList.Count; i++) { //loop through each object

我正在寻找一种将“null”属性值设置为“non-null”值的方法。这些特性与一个对象关联,并且存在多个对象的列表

我遇到的问题是将“null”值转换为“non-null”值,其中每个属性都有不同的类型

到目前为止,我有一些嵌套的循环和条件,试图识别null属性并将它们设置为非null

//loop through each object
for (int i = 0; i < objectList.Count; i++)
{
    //loop through each object and each field within that object
    foreach (var property in objectList[i].GetType().GetProperties())
    {
        var current_field_val = property.GetValue(objectList[i], null);

        //null validation
        if (current_field_val == null)
        {
            PropertyInfo current_field_data_type = objectList[i].GetType().GetProperty(property.Name);

            if (current_field_data_type is String)
            {
                objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], "");
            }
            else if (current_field_data_type is int)
            {
                objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], 0);
            }
            else if (current_field_data_type is double)
            {
                objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], 1);
            }
            else if (current_field_data_type is object)
            {
                objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], "");
            }
        }
    }
}
//遍历每个对象
for(int i=0;i

请原谅我的缩进不好,在来回复制时VS没有发挥好作用。

如果您正在寻找一种方法为任何引用类型生成默认的非
null
值,那么恐怕您运气不好。该语言中没有为任何给定引用类型提供非空默认值的通用机制;默认值正好是
null

如果您需要处理的类型集是有限的且可管理的,那么您可能可以对每个特定情况进行编码


无论如何,这看起来很奇怪。你到底想达到什么目的?很可能有更好的方法来解决此问题。

经过一段时间的研究,避免此问题的最佳方法是为将在反序列化过程中创建的对象创建构造函数/默认值,然后使用此问题中描述的设置-。将使用默认构造函数,并忽略空值

objectsList = JsonConvert.DeserializeObject<List<RootObject>>(json_string, new JsonSerializerSettings
     {
                DefaultValueHandling = DefaultValueHandling.Populate,
                NullValueHandling = NullValueHandling.Ignore
      }

 );
objectsList=JsonConvert.DeserializeObject(json_字符串,新的JsonSerializerSettings
{
DefaultValueHandling=DefaultValueHandling.Populate,
NullValueHandling=NullValueHandling.Ignore
}
);

感谢@Damian的清理编辑!你的问题到底是什么?什么东西没有按预期工作?顺便说一句,这似乎是一个非常糟糕的主意。此外,整数和double不能为null,除非它们被设置为可为null的整数和double。从根本上说,您在设计的早期就犯了错误,这是一个令人讨厌的黑客尝试清理它,因为我的JSON映射到我的对象的方式,只要有一个空白字段-使用null(这是意料之中的)。现在,我希望用一个适合属性数据类型的值来替换所有空值(防止强制转换错误),但是,我似乎无法找到一个有效的方法来替换1。检查具体的数据类型和2。设置值时没有强制转换错误。为什么不在json的反序列化中处理此问题?问题是我正在使用对象(从json反序列化)生成一些HTML,但是如果存在空值,则无法生成HTML。因此,我尝试在生成HTML之前删除空值,而不是在生成HTML时检查每个单独属性的空值。我可以在2和2之间选择,从这个开始。