[英]Code Contracts 1.4.40602.0 - Contract.ForAll doesn't seem to be working?
Warning 1 CodeContracts: requires unproven: Contract.ForAll(coll, item => item != null) C:\\MyApp\\MyObj.cs
public MyObj()
: this(new Collection<Object>()) { }
public MyObj(ICollection<Object> coll)
{
Contract.Requires<ArgumentNullException>(coll != null);
Contract.Requires<ArgumentException>(Contract.ForAll(coll, item => item!= null));
_coll = coll;
}
I realize that in older versions of CodeContracts the Contract.ForAll()
method wasn't supported, but I thought by now (ver 1.4.40602.0) it would be? 我意识到在旧版本的CodeContracts中,不支持Contract.ForAll()
方法,但是我认为现在(版本1.4.40602.0)可以吗? Am I just doing something wrong here or is it still not supported? 我是在这里做错什么还是还是不支持?
I have found the CodeContracts bits suffer from a lack of language and framework integration. 我发现CodeContracts位缺少语言和框架集成。
In this case, the collection you're passing in clearly passes the condition, yet either due to lack of C# language integration (Code Contracts doesn't understand you're passing in an empty collection via C#), or lack of .NET framework integration (the Collection class isn't annotated with code contracts). 在这种情况下,您要传递的集合显然可以通过条件,但是由于缺少C#语言集成(代码协定无法理解您要通过C#传递空集合)或缺少.NET框架集成(Collection类未附带代码契约)。
I have no warnings with 'Warning level' set to 'low' in the CC options. 我没有在CC选项中将“警告级别”设置为“低”的警告。 With the value set to 'high' I got the warning. 将值设置为“高”时,我得到了警告。
I've tried System.Collections.ObjectModel.Collection<T>
and System.Collections.Generic.List<T>
and both give the same warnings. 我已经尝试了System.Collections.ObjectModel.Collection<T>
和System.Collections.Generic.List<T>
并且都给出了相同的警告。
I've tried both constructors and regular method calls - no difference. 我已经尝试了构造函数和常规方法调用-没什么区别。
I've tried 我试过了
public MyObj() : this(new List<Object>()) { }
and 和
public MyObj() : this(new List<Object>{1}) { }
and again no difference. 再次没有区别。
Extracting a variable when doing a regular method call doesn't help either. 在执行常规方法调用时提取变量也无济于事。
Even Assume
didn't help: 甚至Assume
也无济于事:
public void M1()
{
var list = new List<Object>
{
1
};
Contract.Assume(Contract.ForAll(list, t => t != null));
this.X(list); // Still gives warning on the ForAll requirement
}
public void X(ICollection<object> c)
{
Contract.Requires<ArgumentNullException>(c != null);
Contract.Requires<ArgumentException>(Contract.ForAll(c, x => x != null));
}
(I'm using the same CC: 1.4.40602.0 on VS2010 SP1) (我在VS2010 SP1上使用相同的CC:1.4.40602.0)
UPDATE UPDATE
Worked with an array. 使用数组。
Maybe, Judah Himango is right about the lack of contracts on Collection
and List
. 也许Judah Himango对Collection
和List
缺乏合同是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.