如何强制执行空检查?
- 作者: 半萌半骚长发及腰
- 来源: 51数据库
- 2022-10-20
问题描述
我正在从事一个大型项目,即使有成百上千的自动化测试和 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#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
