启动并运行代码合约
在 VS2010 和 .NET 4.0 中,我在智能感知中看到了用于将合同添加到我的代码中的快捷方式(例如 cr、crr),但是当我通过 Tab 键添加这些合同时,代码(例如 Contract.Requires)没有有效的程序集,因此没有智能感知(基本上找不到类型)。
我到底如何启动并运行代码合约?
编辑:所有方法都存在于 System.Diagnostics.Contracts 中,但我认为我会在整个过程中使用属性?此外,可用合同有很多不同的 .dll!
谢谢
In VS2010 and .NET 4.0, I see the shortcuts in intellisense for adding contracts to my code (Eg cr, crr) but when I tab to add these in, the code (Such as Contract.Requires) does not have the valid assembly so there is no intellisense (The type can't be found basically).
How exactly do I get up and running with code contracts?
EDIT: All the methods exist in System.Diagnostics.Contracts, but I thought that I would be using attributes throughout? Also, there are so many different .dlls for the contracts available!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该程序集只是
mscorlib
- 而Contract
位于System.Diagnostics.Contracts
命名空间中。<插头>
有关代码契约的更多信息,您可以购买《C# 深度》第二版并阅读第 15 章。(该章是免费提供的,但恐怕现在不是了。)
或者当然您也可以阅读文档,因为它们非常好:)
如果您发现缺少
System.Diagnostics.Contracts
命名空间,值得检查您是否确实针对 .NET 4 - 如果您在 VS2010 中创建 .NET 3.5 项目,则不会有可用的代码契约(无论如何,无需添加显式程序集引用)。The assembly is just
mscorlib
- andContract
is in theSystem.Diagnostics.Contracts
namespace.<plug>
For some more information about Code Contracts, you could buy the second edition of C# in Depth and read chapter 15. (That chapter was available free, but isn't now I'm afraid.)
</plug>
Or of course you could read the docs too, as they're pretty good :)
If you find you're missing the
System.Diagnostics.Contracts
namespace, it's worth checking that you really are targeting .NET 4 - if you create a .NET 3.5 project in VS2010, that won't have Code Contracts available (without adding an explicit assembly reference, anyway).