无法扩展方法的合同,因为它的原始定义是基类型 [英] Cannot extend contract for method because its original definition is on a base type

查看:77
本文介绍了无法扩展方法的合同,因为它的原始定义是基类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您为派生类中的方法提供额外的帖子条件时,新版本的代码合约会抱怨。

The new version of code contracts complains when you give extra post conditions to methods in derived classes.

例如,我有一个IRandomReadableStream类,它来自IReadableStream。 IRandomReadableStream具有流中的位置概念,因此需要在Read方法上使用额外的post条件来说明位置增加。

For example, I have a class IRandomReadableStream which is derived from IReadableStream. IRandomReadableStream has the notion of position within the stream, and thus needs an extra post condition on the Read method saying that the position increases.

我应该能够将后置条件添加到派生方法中。当调用者知道它正在处理的值是派生类型然后他们可以使用附加信息。

I should be able to add post conditions to derived methods. When a caller knows the value it is working on is the derived type then they can use the additional information.

推荐答案

嗯,我我无法重现这一点。你能给我一个(小!)复制品吗?这是我刚试过的,没有看到任何错误。我尝试使用隐式接口实现,也使用基类和子类。

Hmm, I'm not able to reproduce this. Can you please send me a (small!) repro? Here's what I just tried and didn't see any error. I tried it with an implicit interface implementation and also with a base class and a subclass.


 public partial interface J {
  int M(int x);
 }

 #region J contract binding
 [ContractClass(typeof(JContract))]
 public partial interface J {

 }

 [ContractClassFor(typeof(J))]
 abstract class JContract : J {
  public virtual int M(int x) {
   Contract.Requires(0 < x);
   Contract.Ensures(Contract.Result<int>() >= x);
   throw new NotImplementedException();
  }
 }
 #endregion

 public class C : J {
  int J.M(int x) {
   Contract.Ensures(Contract.Result<int>() >= x + 2);
   return x + 2;
  }
 }


这篇关于无法扩展方法的合同,因为它的原始定义是基类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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