-2

这是代码:

foreach (var property in this.allProperties)
        {
            var propertyItself = element.GetType().GetProperty(property.GetType().Name);

            if (propertyItself.PropertyType != typeof(Int32)) // Here I get System.NullReferenceException: Object reference not set to an instance of an object
            { continue; }

            if ((int)propertyItself.GetValue(element, null) == 0)
            { return false; }
        }

我想不通。如果有人可以或了解发生了什么,请帮助我们!提前致谢!!!

4

2 回答 2

1

propertyItself变量为空。

这意味着这个调用在某种程度上是不正确的:

element.GetType().GetProperty(property.GetType().Name);

我只是在猜测,但我敢打赌这段代码property.GetType().Name应该是property.ToString()或者property.Name如果这是一个选项。

你传入的是类型的名称,而property不是它的Name

于 2013-10-19T19:21:58.370 回答
0

如果没有任何调试器信息,就无法对错误给出具体答案。

试着放

if(propertyIteself!=null && propertyIteslef.PropertyType!=null && propertyItself.PropertyType != typeof(Int32)) 
 { continue; }

这将对可能在该线上爆炸的两个项目进行空检查。

或试试这个

        foreach (var property in this.allProperties)
        {
          var propertyItself = element.GetType().GetProperty(property.GetType().Name);
          if(propertyItself!=null && propertyItself.PropertyType!=null)
           {
                if (propertyItself.PropertyType != typeof(Int32)) 
                { continue; }

                if ((int)propertyItself.GetValue(element, null) == 0)
                 { return false; }
            }
         }
于 2013-10-19T19:19:27.710 回答