代码之家  ›  专栏  ›  技术社区  ›  Diadistis

代码契约问题

  •  1
  • Diadistis  · 技术社区  · 14 年前

    考虑以下代码:

    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);
        }
    }
    

    这确保了 randomString RandomMethod 被执行。为什么代码契约分析会忽略这一事实并抛出 CodeContracts: requires unproven: replacement != null 警告?

    1 回复  |  直到 14 年前
        1
  •  4
  •   Pascal Cuoq    14 年前

    与其说分析器忽略了这一事实,还不如说它无法建立这两个方法之间的联系。

    属性“field randomString is non null”是 类不变项 :它在每个实例创建时建立,并在每个方法调用时简单地保留,因为该字段是只读的。我强烈要求你提供一份声明。它将很容易被分析仪验证,并将提供必要的假设来证明这一点 RandomMethod

    This page