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