本文介绍了Assert / Assume的另一个变种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们要在启用运行时检查的情况下部署一些代码dll。期望的检查级别==前提条件。

我建议再添加一个Assert / Assume变量(Contract.Verify()用于examaple),其行为与Assume完全相同,但如果我们编译检查级别!=完整。这就是我们如何使用它:


Suppose we want to deploy some code dll with runtime checking enabled. Desired checking level == Preconditions.

I would suggest to add one more variant of Assert/Assume (Contract.Verify() for examaple), which will behave exactly like Assume, but will remain in assembly if we compile with checking levels != Full. This is how we might use it:

void Test()
{
int x = CallExternalLib(); //被叫方法没有契约
    void Test()
    {
        int x = CallExternalLib(); // called method has no contracts

推荐答案

void验证(布尔条件){

void Verify(bool condition) {

   Contract.Ensures(条件); //这是为了让静态检查员知道这个帮助者在呼叫站点的含义

   Contract.Ensures(condition); // this is so that the static checker knows about the meaning of this helper at the call site

   if(!condition)throw ....

   if (!condition) throw ....

}


这篇关于Assert / Assume的另一个变种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 23:14