如何强制执行空检查?

How do I enforce null checking?(如何强制执行空检查?)
本文介绍了如何强制执行空检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在从事一个大型项目,即使有成百上千的自动化测试和 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.)

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

ActiveDirectory error 0x8000500c when traversing properties(遍历属性时 ActiveDirectory 错误 0x8000500c)
search by samaccountname with wildcards(使用通配符按 samaccountname 搜索)
Get the list of Groups for the given UserPrincipal(获取给定 UserPrincipal 的组列表)
Can you find an Active Directory User#39;s Primary Group in C#?(你能在 C# 中找到 Active Directory 用户的主要组吗?)
Query From LDAP for User Groups(从 LDAP 查询用户组)
How can I get DOMAINUSER from an AD DirectoryEntry?(如何从 AD DirectoryEntry 获取 DOMAINUSER?)