警告 1 CodeContracts:需要未经验证: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)会支持吗?我只是在这里做错了什么还是仍然不支持?
最佳答案
我在 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)
更新
使用数组。
也许,Judah Himango 关于Collection
缺乏契约(Contract)的说法是正确的。和List
.
关于c# - 代码契约(Contract) 1.4.40602.0 - Contract.ForAll 似乎不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8448615/