为什么不包含的NullReferenceException什么是空的信息? [英] Why doesn't NullReferenceException contain information about what is null?

查看:218
本文介绍了为什么不包含的NullReferenceException什么是空的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是落后的NullReferenceException不包含除基础类数据(如堆栈跟踪)的任何运行时的具体信息,设计决策?而且是有Visual Studio的一个扩展,它可以告诉你马上其中前pression的部分是空?

What was the design decision behind NullReferenceException not containing any runtime specific information except base class data (like a stacktrace)? And is there an extension for Visual Studio that can tell you straight away which part of an expression was null?

推荐答案

这是NRE是一个非常低级的异常。它是由处理器产生的,当它被要求从下面64K的地址读取的数据的硬件异常(一个陷阱)。虚拟内存空间,该地区一直未映射,专门陷阱指针错误。它开始作为一个AccessViolation,并得到由CLR变成NRE当地址小于0x00010000在。在这一点上没有为例外极少情况下,这是所有已知的是导致陷阱机器code指令的地址。

An NRE is a very low-level exception. It is a hardware exception (a 'trap') generated by the processor when it is asked to read data from an address below 64K. That region of the virtual memory space is always unmapped, specifically to trap pointer bugs. It starts as an AccessViolation and gets turned into NRE by the CLR when the address is less than 0x00010000. At that point there is very little context for the exception, all that's known is the address of the machine code instruction that caused the trap.

逆向工程的机器code指令地址返回给程序中的一个命名的变量是不可能的。重要的是,它的工作原理是这样,抖动将必须产生的非常的低效code否则。所有这一切可以合理地做的是恢复源$ C ​​$ C行号。这需要一个包含行号信息的调试信息(使用.pdb)。在CLR知道如何读取.pdb文件,并使用它来生成异常的堆栈跟踪。然而,这往往是由于由JIT优化进行优化仍然不准确的,它的动作code左右。你将只能得到有保证的匹配调试版本。为什么发布版本的PDB不包含源代码行号信息的原因。您可以更改。

Reverse-engineering that machine code instruction address back to a named variable in your program isn't possible. It is important that it works that way, a jitter would have to generate very inefficient code otherwise. All that can be reasonably done is recover the source code line number. That requires debugging info (a .pdb) that contains line number info. The CLR knows how to read the .pdb file and uses it to generate the exception's stack trace. However, this is often still inaccurate due to optimizations performed by the JIT optimizer, it moves code around. You'll only get a guaranteed match for the Debug build. The reason why the PDB for the Release build doesn't contain source line number info. You can change that.

这问题有一个非常简单的解决方案。检查空自己,让运行时做到这一点之前,产生自己的异常。该测试是相当便宜,还有不到一纳秒。

This problem has a very simple solution. Check for null yourself and generate your own exception before letting the runtime do it. The test is quite cheap, well less than a nanosecond.

这篇关于为什么不包含的NullReferenceException什么是空的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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