接口契约中的[Pure]方法 [英] [Pure] methods within interface contract
本文介绍了接口契约中的[Pure]方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做类似的事情:
[ContractClass(typeof(ISomethingContract))]
public interface ISomething
{
void SomeMethod();
}
[ContractClassFor(typeof(ISomething))]
public abstract class ISomethingContract : ISomething
{
[Pure]
public bool SomeComplicatedLogic()
{
return true;
}
public void SomeMethod()
{
Contract.Requires(SomeComplicatedLogic());
}
}
当我尝试时,我收到以下错误:
When I try that, I get the following error:
合同类'ConsoleApplication2.ISomethingContract'引用成员'ConsoleApplication2.ISomethingContract.SomeComplicatedLogic',它不是要注释的抽象类/接口的一部分。
Contract class 'ConsoleApplication2.ISomethingContract' references member 'ConsoleApplication2.ISomethingContract.SomeComplicatedLogic' which is not part of the abstract class/interface being annotated.
推荐答案
从我的臀部拍摄:使复杂的逻辑静态并传入一个实例(界面,而不是抽象类)。
Shooting from my hip: make complicated logic static and pass in an instance (of the interface, not the abstract class).
这篇关于接口契约中的[Pure]方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文