微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在使用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调用放在方法的顶部,就不会出现任何问题.

原文地址:https://www.jb51.cc/csharp/92843.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐