我正在从事一个大型项目,即使有成百上千的自动化测试和 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.)
这篇关于如何强制执行空检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!