我正在将3.5库用于Microsoft代码契约(Contract)

public object RetrieveById(int Id)
{
    //stuff happens...
    Contract.Ensures(newObject != null, "object must not be null");
    return newProject;
    //No error message if I move the Contract.Ensures to here
    //But it isn't asserting/throwing a contract exception here either
}

我得到编译器消息:
“方法'Controller.RetrieveById(System.Int32)”中try块内的错误18 Contract部分

更新:

我在您的帮助下找到了答案:
  • 移至顶部
  • 检查Contract.Result

    Contract.Ensures(Contract.Result()!= null,“对象不能为null”);
  • 最佳答案

    我可能会丢失一些东西,但是我只是看了一下文档:

    http://msdn.microsoft.com/en-us/library/dd412865.aspx

    它说:



    因此,只需将“确保”调用留在方法的顶部,就不会出现任何问题。

    关于c# - 尝试块中的 “Contract can'是什么意思?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2883572/

    10-14 00:32