有没有办法定义全局Contract.Requires对象中所有方法的前提条件? [英] Is there a way to define a global Contract.Requires precondition for all methods in an object?

查看:54
本文介绍了有没有办法定义全局Contract.Requires对象中所有方法的前提条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所要求的基本上是一个ObjectInvariant方法,用于检查每次调用的开头的不变量
,而不是最后一次。

What I am asking is basically an ObjectInvariant method that checks the invariants at the beginning of each call, instead of at the end.

 

这个问题的原因是因为我正在尝试实现一个实现IDisposable的对象。  为了确保客户端在调用Dispose()之后没有调用我的方法,我在每个
单个方法上都有一个Contract.Requires(IsDisposed == false),甚至是Dispose()和我的Finalizer 。  然而,在每个方法中手动输入Contract.Requires调用真的很乏味(更不用说我可能忘记将它添加到我定义的新方法中!)。

The reason for this question is because I am trying to implement an object that implements IDisposable.  In order to make sure that clients aren't calling my methods AFTER they call Dispose(), I have a Contract.Requires(IsDisposed == false) on every single method, even Dispose() and my Finalizer.  However, it is really tedious to manually put in the Contract.Requires call in every method (not to mention I might forget to add it to new methods I define!).

  ;

我不能放入一个ObjectInvariant来自动检查每个方法调用,因为它在方法的末尾而不是开头检查它。  有没有办法为我的对象中的所有方法指定全局Contract.Requires方法?

I can't put in an ObjectInvariant to auto check every method call, because it checks it at the end of the method, not the beginning.  Is there a way to just specify a global Contract.Requires method for all methods in my object?

推荐答案

您可以使用缩写词功能(手册中的第2.12节,但这仍然意味着在所有方法的开头都进行某种方法调用。
You can use the abbreviator functionality (Section 2.12 in the manual), but that still means putting some kind of method call at the beginning of all of your methods.


这篇关于有没有办法定义全局Contract.Requires对象中所有方法的前提条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆