问题描述
我想什么,对我来说,似乎是一些相当基本的code合同code。我已经减少它到下面的问题。下面将失败的静态分析,用消息
使用系统;
使用System.Diagnostics.Contracts;
命名空间PlayAreaCollection2010
{
公共类StrippedContract:IBASIC
{
私人布尔_frozen = FALSE;
公共无效冻结()
{
_frozen = TRUE;
}
公共BOOL冷冻{{返回_frozen; }}
}
[ContractClass(typeof运算(IBasicContract))]
公共接口IBASIC
{
无效冻结();
布尔冷冻{获得; }
}
[ContractClassFor(typeof运算(IBASIC))]
公共抽象类IBasicContract:IBASIC
{
#地区IBASIC会员
公共无效冻结()
{
Contract.Ensures(this.Frozen);
}
公共BOOL冰冻
{
获得{返回true;}
}
#endregion
}
}
然而,细了以下工作,并满足所有检查:
使用系统;
使用System.Diagnostics.Contracts;
命名空间PlayAreaCollection2010
{
公共类StrippedContract
{
私人布尔_frozen = FALSE;
公共无效冻结()
{
Contract.Ensures(this.Frozen);
_frozen = TRUE;
}
公共BOOL冷冻{{返回_frozen; }}
}
}
什么我需要做的,以满足静态检查,当我把我的合同在界面?
在你执行 IBASIC
, StrippedContract
的,您将需要一个后置条件添加到冷冻
属性:
公共BOOL冷冻{
得到 {
Contract.Ensures(Contract.Result<布尔>()== this._frozen);
返回_frozen;
}
}
另外,你可以在你的项目的属性的code合同选项卡中添加 -infer
命令行选项来静态检查。这将允许静态检查自动推断出这一点。
I'm trying what, to me, seems like some fairly basic code contracts code. I've reduced it down to the following problem. The following fails the static analysis, with the message
using System;
using System.Diagnostics.Contracts;
namespace PlayAreaCollection2010
{
public class StrippedContract : IBasic
{
private bool _frozen = false;
public void Freeze()
{
_frozen = true;
}
public bool Frozen { get { return _frozen; } }
}
[ContractClass(typeof(IBasicContract))]
public interface IBasic
{
void Freeze();
bool Frozen { get; }
}
[ContractClassFor(typeof(IBasic))]
public abstract class IBasicContract : IBasic
{
#region IBasic Members
public void Freeze()
{
Contract.Ensures(this.Frozen);
}
public bool Frozen
{
get { return true;}
}
#endregion
}
}
However, the following works fine and satisfies all checks:
using System;
using System.Diagnostics.Contracts;
namespace PlayAreaCollection2010
{
public class StrippedContract
{
private bool _frozen = false;
public void Freeze()
{
Contract.Ensures(this.Frozen);
_frozen = true;
}
public bool Frozen { get { return _frozen; } }
}
}
What do I need to do to satisfy the static checker, when I've placed my contracts in the interface?
In your implementation of IBasic
, StrippedContract
, you will need to add a post-condition to the Frozen
property:
public bool Frozen {
get {
Contract.Ensures(Contract.Result<bool>() == this._frozen);
return _frozen;
}
}
Alternatively, you could add the -infer
command line option to the static checker in the Code Contracts tab of your project's properties. That will allow the static checker to infer this automatically.
这篇关于通过属性实现接口时,保证未经证实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!