代码合同问题 [英] CodeContracts issue

查看:23
本文介绍了代码合同问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有一个关于 CodeContracts 的小问题.我有一个类库项目,它作为一个带有方法 foo(string s1, string s2); 的类.在 foo 方法中,我有一个 Contract.Requires(s1 != null).因此,如果我理解我的代码的含义(是的,我刚刚安装了 CodeContracts 并玩了 :),合约将在构建过程和运行时检查 s1 != null 表达式,抛出 ArgumentException>.我想测试行为,当我从类 lib 项目调用 foo(null, "test") 时,设计者告诉我这个问题,但是当我从 winform 应用程序项目调用它时,我没有收到任何警告错误列表窗口.那么这是否意味着代码契约只在它们驻留的项目中起作用而不是在外部?谢谢

Hello I have a little issue regarding CodeContracts. I have a class library project which as a class with a method foo(string s1, string s2); inside the foo method, I have a Contract.Requires(s1 != null). So if I understand the meaning of my code (yes, I just installed CodeContracts and playing with :), the contract will check the s1 != null expression during build process and in runtime, throwing ArgumentException>. I wanted to test the behavior, when I call foo(null, "test") from class lib project, the designer tells me about the issue, but when I call it from the winform app project, I don't get any warnings in error list window. So does this mean that code contracts works only in the project they reside and not outside? Thanks

更新

我忘了提到我添加的先决条件在静态分析中不起作用.但是,它们确实会在运行时抛出带有适当消息的 ArgumentException.

I forgot to mention that the preconditions I have added doesn't work in static analysis. However they do throw ArgumentException with appropriate message in runtime.

推荐答案

好的,感谢 MS 团队,我发现了问题.这种奇怪行为的原因是我的程序集名称以.Contracts.dll"结尾.问题是静态分析器不检查名称以该结尾的程序集.我重命名了程序集,一切都像魅力一样,就像任何其他 MS 产品一样 :)

Ok guys, thanks to MS team, I found the problem. The reason of such a strange behavior was that my assembly's name ended with ".Contracts.dll". And the problem is that static analyzer doesn't check assemblies whose names ends with that. I renamed the assembly and everything works like a charm, just like any other MS product :)

这篇关于代码合同问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆