你如何执行抽象方法的合同。 [英] How do you enforce a contract on abstract methods.

查看:108
本文介绍了你如何执行抽象方法的合同。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个抽象类上定义一个契约,然后继承它,我得到一些与覆盖抽象方法实现有关的未经证实的警告。
所以如果你不能将Requires语句添加到继承的覆盖中方法是否有办法以某种方式强制执行合同。
我认为代码是这样的。


I'm defining a contract on an abstract class and then inheriting from it and I'm getting some unproven warnings relating to overriden abstract method implementations.
So if you can't add Requires statements to the inherited overriden methods is there a way to enforce the contract somehow.
I think the code went something like this.

public class abstract AbstractClass<T>
{
   public void Validate(T obj)
  {
    Contract.Requires(obj!=null);
  }
   
  public abstract DoSomething(T obj);

}

public class ConcreteClass : Abstract<SomeType>
{
  public override DoDomething(T obj)
  {
    Validate(obj);
  }
}

推荐答案

你好,

你需要第三节课,从抽象类派生,您可以在其中定义DoSomething的需求。然后使用ContractClassAttribute和ContractClassForAttribute绑定抽象类和第三个类。

这在代码合同手册中有详细描述。
Hi,

You will need a third class, derived from abstract class, where you can define requires for DoSomething. Then bind abstract class and this third class with ContractClassAttribute and ContractClassForAttribute.

This is described in details in the Code Contracts manual.


这篇关于你如何执行抽象方法的合同。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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