1
考虑下面的代码:代码契约问题
public class RandomClass
{
private readonly string randomString;
public RandomClass(string randomParameter)
{
Contract.Requires(randomParameter != null);
Contract.Ensures(this.randomString != null);
this.randomString = randomParameter;
}
public string RandomMethod()
{
return // CodeContracts: requires unproven: replacement != null
Regex.Replace(string.Empty, string.Empty, this.randomString);
}
}
这保证了当RandomMethod
被执行randomString
将不能为空。为什么代码合同分析忽略了这个事实并且引发CodeContracts: requires unproven: replacement != null
警告?
谢谢队友。添加'ContractInvariantMethod'解决了这个问题:) – Diadistis 2010-09-16 12:48:59