确保实现接口时通过属性未经验证
我正在尝试对我来说似乎是一些相当基本的代码合约代码。我已将其简化为以下问题。以下静态分析失败,并显示消息
CodeContracts:确保未经证实: 这个.冻结
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
}
}
但是,以下工作正常并满足所有检查:
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; } }
}
}
CodeContracts:已检查 1 个断言:1 个正确
当我将合同放入界面中时,我需要做什么才能满足静态检查器的要求?
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
CodeContracts: ensures unproven:
this.Frozen
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; } }
}
}
CodeContracts: Checked 1 assertion: 1 correct
What do I need to do to satisfy the static checker, when I've placed my contracts in the interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
IBasic
、StrippedContract
的实现中,您需要向Frozen
属性添加后置条件:或者,您可以添加 < code>-infer 命令行选项添加到项目属性的“代码合同”选项卡中的静态检查器。这将允许静态检查器自动推断这一点。
In your implementation of
IBasic
,StrippedContract
, you will need to add a post-condition to theFrozen
property: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.