問題描述
我正在從事一個大型項目,即使有成百上千的自動化測試和 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.
有沒有辦法在編譯時強制執(zhí)行空檢查?
Is there any way to enforce null-checking at compile time?
除此之外,有沒有什么方法可以在單元測試中自動強制執(zhí)行空檢查,而不必自己為空情況編寫測試?
Barring that, is there any way to automagically enforce null-checking in unit tests without having to write the tests for null cases myself?
推薦答案
您應(yīng)該查看 代碼合同.靜態(tài)檢查器僅適用于高端 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.
網(wǎng)上有很多資源,<plug>
你還可以閱讀 C# 2nd edition in Depth 中關(guān)于代碼契約的章節(jié)的預(yù)發(fā)布版本 - 免費下載第 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.)
這篇關(guān)于如何強制執(zhí)行空檢查?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!