在我的一种方法开始时,我有一个非常简单的检查,如下所示:
public void MyMethod(MyClass thing)
{
if(thing == null)
throw new ArgumentNullException("thing");
//Do other stufff....
}
但是我得到了堆栈跟踪(来自生产环境中的 Elmah),这似乎表明“if(thing == null)”行正在抛出 NullReferenceException。堆栈跟踪的前 2 行类似于:
System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at MyLibrary.BL.AnotherClass.MyMethod(MyClass thing) in C:\Development\MyProject\trunk\MyLibrary.BL\AnotherClass.cs:line 100
MyClass 是一个相当简单的类,没有运算符重载或类似的东西,所以我对抛出 NullReferenceException 的原因有点困惑!
任何人都可以提出可能导致这种情况的场景吗?
编辑:我怀疑“事物”可能为空,但我真的希望 ArgumentNullException 而不是 NullReferenceException - 这基本上就是这个问题的意义所在。是否有框架或 Elmah 正在改变或错误报告异常 - 或者是二进制文件在某种程度上已经过时的唯一解释?