C# System.NullReferenceException:。。。在C中#
代码如下:C# System.NullReferenceException:。。。在C中#,c#,reflection,C#,Reflection,代码如下: foreach (var property in this.allProperties) { var propertyItself = element.GetType().GetProperty(property.GetType().Name); if (propertyItself.PropertyType != typeof(Int32)) // Here I get System.NullReferenceExc
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; }
}
我想不出来。如果任何人能够或理解正在发生的事情,请帮助我们!提前感谢 没有任何调试器信息,就无法给出错误的具体答案 试一试推杆
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; }
}
}
propertyItself
变量为空
这意味着这个电话有点不正确:
element.GetType().GetProperty(property.GetType().Name);
我只是猜测,但我打赌这个代码property.GetType().Name
应该是property.ToString()
或property.Name
,如果这是一个选项的话
您传入的是
属性类型的名称,而不是它的名称
您有一个空引用异常。。。。这就是问题所在。如果没有堆栈跟踪或某些上下文,我们就无法看到问题所在。请附加一个调试器并进行调查propertyItself
可能为空。每当我有空引用异常时,我调用CSI.)霍雷肖很快就解决了我所有的问题。呵呵…物业本身是空的耶,好的!我将尝试这种方法并发布堆栈跟踪。谢谢很好的观察。我将试着改变你的观点。[编辑]成功了!好极了!令人惊叹的!!!谢谢@canhazbits。