本文介绍了使用“contract.ensures”抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好, 我想用contract.ensures抛出一个特定的例外 例如和要求一样: Contract.Requires< ArgumentNullException>(参数!= null ); 但我不知道如何做同样的确保,是否可能?还是有其他解决方案?只想检查方法的结果。 谢谢你 解决方案 您可以提供自己的方法,在合同失败时在运行时调用。请参阅文档中的第7节以了解如何执行此操作。 没有等效于Requires< ; T> for postconditions。我们有一个通用的前提条件,只是为了尝试兼顾向后兼容性。人们通常已经有参数验证代码,并且觉得他们需要继续抛出相同(无用!)的异常。但是大多数代码都没有后置条件,因此我们认为我们可以推测合同失败不应该被编程。 这有帮助吗? Hello,I'd like to throw a specific exception with contract.ensuresFor example like with requires :Contract.Requires<ArgumentNullException>(argument != null);But I don't see how to do the same with ensures, is it possible ? or is there another solution ? just want to check the result of the method.Thank you 解决方案 You can provide your own methods that get called at runtime when a contract fails. See Section 7 in the documentation for how to do this.There is no equivalent to Requires<T> for postconditions. We have the generic one for preconditions only to try and cater for backward compatibility. People often already have parameter validation code and feel that they need to continue throwing the same (useless!) exceptions. But most code does not already have postconditions, so there we feel we can push our belief that contract failures should not be programmed against.Does that help? 这篇关于使用“contract.ensures”抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 22:27