如何强制执行空检查?

时间:2022-11-09
本文介绍了如何强制执行空检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在从事一个大型项目,即使有成百上千的自动化测试和 100% 的代码覆盖率,我们也会遇到大量错误.我们得到的大约 95% 的错误都是 NullReferenceExceptions.

I'm working on a large project where, even with 10s of 1000s of automated tests and 100% code coverage, we're getting a ridiculous number of errors. About 95% of errors we get are NullReferenceExceptions.

有没有办法在编译时强制执行空检查?

Is there any way to enforce null-checking at compile time?

除此之外,有没有什么方法可以在单元测试中自动强制执行空检查,而不必自己为空情况编写测试?

Barring that, is there any way to automagically enforce null-checking in unit tests without having to write the tests for null cases myself?

推荐答案

您应该查看 代码合同.静态检查器仅适用于高端 VS 版本,但这基本上是您所追求的.

You should look into Code Contracts. The static checker is only available for the higher-end VS editions, but that's basically what you're after.

网上有很多资源,<plug> 你还可以阅读 C# 2nd edition in Depth 中关于代码契约的章节的预发布版本 - 免费下载第 15 章.</plug>(这一章相对于最新和最伟大的代码契约版本来说有点过时,但没什么大不了的.)

There are plenty of resources online, and <plug> you can also read a prerelease version of the chapter on Code Contracts from the 2nd edition of C# in Depth - download chapter 15 for free. </plug> (The chapter is slightly out of date with respect to the latest and greatest build of Code Contracts, but nothing huge.)

这篇关于如何强制执行空检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:C#:如何实现和使用 NotNull 和 CanBeNull 属性 下一篇:捕获的异常本身为空!

相关文章

最新文章