[英]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;
}
我意识到在旧版本的CodeContracts中,不支持Contract.ForAll()
方法,但是我认为现在(版本1.4.40602.0)可以吗? 我是在这里做错什么还是还是不支持?
我发现CodeContracts位缺少语言和框架集成。
在这种情况下,您要传递的集合显然可以通过条件,但是由于缺少C#语言集成(代码协定无法理解您要通过C#传递空集合)或缺少.NET框架集成(Collection类未附带代码契约)。
我没有在CC选项中将“警告级别”设置为“低”的警告。 将值设置为“高”时,我得到了警告。
我已经尝试了System.Collections.ObjectModel.Collection<T>
和System.Collections.Generic.List<T>
并且都给出了相同的警告。
我已经尝试了构造函数和常规方法调用-没什么区别。
我试过了
public MyObj() : this(new List<Object>()) { }
和
public MyObj() : this(new List<Object>{1}) { }
再次没有区别。
在执行常规方法调用时提取变量也无济于事。
甚至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));
}
(我在VS2010 SP1上使用相同的CC:1.4.40602.0)
UPDATE
使用数组。
也许Judah Himango对Collection
和List
缺乏合同是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.