加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – “契约不能在试块中”是什么意思?

发布时间:2020-12-15 04:05:28 所属栏目:百科 来源:网络整理
导读:我正在使用3.5库来进行微软代码合同 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 i
我正在使用3.5库来进行微软代码合同
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.Result

Contract.Ensures(Contract.Result()!= null,“object不能为null”);

解决方法

我可能会遗漏一些东西,但我只是看了一下这个文档:

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

它说:

This method call must be at the
beginning of a method or property,
before any other code.

因此,只需将Ensures调用放在方法的顶部,就不会出现任何问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读