我是代码合同的新手.我下载了最新版本的代码合同项目(1.4.40314.1)并开始在我的项目中实现它.当我通过VS2010中的代码合同选项卡启用"Runtume Checking"时,出现此错误
Error 1 The command ""C:\Program Files (x86)\Microsoft\Contracts\Bin\ccrewrite" "@Application1ccrewrite.rsp"" exited with code -1.
每次我建立项目.Plz的帮助.
现在这对我来说是个主要问题.使用代码契约的每个项目在VS2010错误窗口中显示相同的错误,在输出窗口中找不到'Application1ccrewrite.rsp',但它就在那里.
我尝试了一切.我安装了两个版本(Pro,Std),但问题仍然存在.Plz帮忙!

我正在尝试将代码契约应用于我的代码,我遇到了一个令人困惑的问题.这段代码无法满足合同,但除非我真的很厚,否则我希望它能够轻松分析id在返回时必须有一个值
Run Code Online (Sandbox Code Playgroud)if (id == null) throw new InvalidOperationException(string.Format("{0} '{1}' does not yet have an identity", typeof(T).Name, entity)); return id.Value;

我有一个IEnumerable非空的参数.如果有一个前提条件,如下面的那个,那么集合将在它期间枚举.但是下次我引用时会再次列举它.(Resharper中的"可能多次枚举IEnumerable"警告.)
void ProcessOrders(IEnumerable<int> orderIds)
{
Contract.Requires((orderIds != null) && orderIds.Any()); // enumerates the collection
// BAD: collection enumerated again
foreach (var i in orderIds) { /* ... */ }
}
Run Code Online (Sandbox Code Playgroud)
这些变通办法使Resharper高兴但不会编译:
// enumerating before the precondition causes error "Malformed contract. Found Requires
orderIds = orderIds.ToList();
Contract.Requires((orderIds != null) && orderIds.Any());
---
// enumerating during the precondition causes the same error
Contract.Requires((orderIds != null) && (orderIds = orderIds.ToList()).Any());
Run Code Online (Sandbox Code Playgroud)
还有其他一些有效但可能并不总是理想的解决方法,比如使用ICollection或IList,或执行典型的if-null-throw-exception.
是否有一个解决方案适用于代码契约和IEnumerables,如在原始示例中?如果没有,那么有人制定了一个好的模式来解决它吗?
我一直在Visual Studio Express 2013中开发一个C#项目,并且遇到了.NET语言的代码契约.他们的简洁性和随附的静态分析工具给我留下了深刻的印象,我开始在我的代码库中使用它们.但是,当我试图运行我的程序时,我遇到了类似于在这个SO线程中发现的错误消息,即
...必须使用代码契约二进制重写器(CCRewrite)重写程序集(可能是"<my project>"),因为它正在调用Contract.Requires并且定义了CONTRACTS_FULL符号.从项目中删除CONTRACTS_FULL符号的任何显式定义并重建...
指南建议要解决此问题,我必须从项目的"属性"页面启用代码约定,但在Express中无法找到代码约定属性条目.
一些MSDN论坛主题似乎表明代码合同的所有工具都包含在Express版本中,但Code Contracts Properties页面却没有.这似乎是这种情况,因为我能够在VSE 2013中运行我的项目,只有在我的大学毕业之前从我的大学获得的Visual Studio 2012 Ultimate的副本启用代码合同之后.
除了通过手动修改项目文件或使用付费版本的Visual Studio修改项目文件外,是否真的无法在Visual Studio Express中使用代码约定?如果是这种情况,我对使用它们非常犹豫,因为我的公司不太可能购买VS许可证.此外,微软试图扩展这种新的优越的验证范例,然后将其仅限于付费用户,这似乎是非常奇怪的.
.net c# code-contracts visual-studio-express visual-studio-2013
考虑以下.
private static void BindClassesToInterfacesByConvention(string classesEndingWith
, string interfacesEndingwith) {
Contract.Requires<ArgumentNullexception>(
string.IsNullOrWhiteSpace(classesEndingWith)
, "classesEndingWith");
Contract.Requires<ArgumentNullException>(
string.IsNullOrWhitespace(interfacesEndingWith)
, "interfacesendingWith");
...
}
Run Code Online (Sandbox Code Playgroud)
我发现它比简单地使用一个更令人困惑 if statement
private static void BindClassesToInterfacesByConvention(string classesEndingWith
, string interfacesEndingwith) {
if (string.IsNullOrWhiteSpace(classesEndingWith))
throw new ArgumentNullException("classesEndingWith");
if (string.IsNullOrWhitespace(interfacesEndingWith))
throw new ArgumentNullException("interfacesendingWith");
...
}
Run Code Online (Sandbox Code Playgroud)
代码合同应该在编译时警告我合同违反.因此,当我写下以下内容时,我期待得到错误或警告.
BindClassesToInterfacesByConvention(null, null);
Run Code Online (Sandbox Code Playgroud)
没有任何事情发生,一切编译得很好,既没有出现错误也没有出现警告信息.
在这种情况下,我认为最好继续使用it statement.或者也许是不公平的使用Code Contracts?
.NET 4引入了Code Contracts作为一项新功能.我想使用CC,但提供仍可在3.5 SP1中运行的文件.那可能吗?我可以只使用部分新功能吗?显然有可能让CC只进行静态检查而不包含在二进制文件中,这是正确的吗?
我知道CC可用于3.5作为单独的模块,这是一个可行的解决方法,以防我无法使用4版本的CC?功能集有区别吗?
我正在为学校目的实现我自己的ArrayList,但为了调整一些东西,我正在尝试使用C#4.0代码契约.一切都很好,直到我需要将Contracts添加到构造函数中.我应该在空参数构造函数中添加Contract.Ensures()吗?
public ArrayList(int capacity) {
Contract.Requires(capacity > 0);
Contract.Ensures(Size == capacity);
_array = new T[capacity];
}
public ArrayList() : this(32) {
Contract.Ensures(Size == 32);
}
Run Code Online (Sandbox Code Playgroud)
我会说是的,每种方法都应该有明确的合同.另一方面,如果它只是将工作委托给"主"构造函数,为什么要这样做呢?逻辑上,我不需要.
我认为在两个构造函数中明确定义合同有用的唯一方面是,如果将来我们对合同有Intelisense支持.如果发生这种情况,明确每种方法所具有的合同是非常有用的,因为它出现在Intelisense中.
此外,是否有任何书籍更深入地了解合同设计的原则和用法?有一件事是了解如何在一种语言中使用Contracts的语法(在本例中为C#),另一种是知道如何以及何时使用它.我阅读了几篇教程和Jon Skeet的C#深度文章,但是如果可能的话,我想更深入一些.
谢谢
几年前,在.NET 4发布之前,代码合同遍布整个博客圈,.NET 4中包含的运行时组件以及更昂贵的Visual Studio 2010版本中提供的静态检查程序.
代码合同的嗡嗡声似乎已经消失,人们是否在生产中使用它?微软研究院还在做更多的工作吗?
我有一些工厂方法
public T Create<T> () where T : class
{
Contract.Ensures(Contract.Result<T>() != null);
T result = this.unityContainer.Resolve<T>();
return result;
}
Run Code Online (Sandbox Code Playgroud)
我尝试构建项目,我得到警告:
CodeContracts:确保未经证实:Contract.Result()!= null
我知道IUnityContainer接口没有任何契约,因此Code Contracts认为varible可能为null,并且无法证明Create()将返回非null结果.
在这种情况下,我如何使代码契约相信结果变量不为空?
我首先尝试调用Contract.Assert
public T Create<T> () where T : class
{
Contract.Ensures(Contract.Result<T>() != null);
T result = this.unityContainer.Resolve<T>();
Contract.Assert(result != null);
return result;
}
Run Code Online (Sandbox Code Playgroud)
但它又向我发出警告:
CodeContracts:断言未经证实
我尝试检查null,这使得所有警告都消失了:
public T Create<T> () where T : class
{
Contract.Ensures(Contract.Result<T>() != null);
T result = this.unityContainer.Resolve<T>();
if (result == null)
{
throw new InvalidOperationException();
}
return …Run Code Online (Sandbox Code Playgroud) 我正在使用代码合同(实际上,使用这个来学习).
我面临一些奇怪的事情......我重写了一个在第三方程序集中定义的方法.我想添加一个Contract.Require这样的语句:
public class MyClass: MyParentClass
{
protected override void DoIt(MyParameter param)
{
Contract.Requires<ArgumentNullException>(param != null);
this.ExecuteMyTask(param.Something);
}
protected void ExecuteMyTask(MyParameter param)
{
Contract.Requires<ArgumentNullException>(param != null);
/* body of the method */
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到这样的警告:
警告1 CodeContracts:方法'MyClass.DoIt(MyParameter)'覆盖'MyParentClass.DoIt(MyParameter))',因此无法添加Requires.
[编辑]稍微更改了代码以显示替代问题[/ edit]
如果我删除了Contract.RequiresDoIt方法,我会收到另一个警告,告诉我必须提供未经证实的param != null
我不明白这个警告.原因是什么,我可以解决吗?
code-contracts ×10
c# ×7
.net ×4
c#-4.0 ×2
.net-3.5 ×1
.net-4.0 ×1
ienumerable ×1
if-statement ×1
validation ×1