如何避免NullReferenceException [英] How to avoid a NullReferenceException

查看:110
本文介绍了如何避免NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  if(alMethSign [z] .ToString()。包含(aClass.Namespace))

这里,我加载一个exe或dll并检查其命名空间。在某些dll中,没有命名空间,所以 aclass.namespace 不存在,它抛出一个 NullReferenceException 。 p>

我必须避免它,它应该继续其余的代码。如果我使用try-catch,它执行catch部分;我想要继续其余的代码。

解决方案

aClass a 类型实例?如果是这样 - 只需检查它为null:

  if(aClass!= null&& alMethSign [z] .ToString ).Contains(aClass.Namespace))


 if (alMethSign[z].ToString().Contains(aClass.Namespace))

Here, I load an exe or dll and check its namespace. In some dlls, there is no namespace, so aclass.namespace is not present and it's throwing a NullReferenceException.

I have to just avoid it and it should continue with rest of the code. If I use try-catch, it executes the catch part; I want it to continue with the rest of the code.

解决方案

Is aClass a Type instance? If so - just check it for null:

if (aClass != null && alMethSign[z].ToString().Contains(aClass.Namespace))

这篇关于如何避免NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆