首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Func<>上定义代码契约

在Func<>上定义代码契约
EN

Stack Overflow用户
提问于 2014-03-07 07:56:27
回答 1查看 92关注 0票数 2

我正在尝试将Code Contracts集成到一些现有的代码中,到目前为止,大部分都很好。然而,我面临的问题是,我在一个接口上设置了契约,并且一个实现将实现委托给一个Func<>,该an被传递到类的构造函数中,如下所示:

代码语言:javascript
运行
复制
[ContractClass(typeof(IFooContract))]
public interface IFoo
{
    object Bar();
}

[ContractClassFor(typeof(IFoo))]
public abstract class IFooContract : IFoo
{
    object IFoo.Bar()
    {
        Contract.Ensures(Contract.Result<object>() != null);
        throw new NotImplementedException();
    }
}

public class DelegatedFoo : IFoo
{
    public DelegatedFoo(Func<object> barImplementation)
    {
        Contract.Requires(barImplementation != null);
        _barImplementation = barImplementation;
    }
    private readonly Func<object> _barImplementation;

    [ContractInvariantMethod]
    private void ObjectInvariants()
    {
        Contract.Invariant(_barImplementation != null);
    }

    public object Bar()
    {
        //"ensures unproven: Contract.Result<object>() != null" here.
        return _barImplementation();
    }
}

静态分析器报告错误“确保未验证: Contract.Result() != null”。我可以在Func<> (以及Action<>上的扩展)上定义合同吗?

这是最好的解决方案吗?

代码语言:javascript
运行
复制
public class DelegatedFoo : IFoo
{
    public DelegatedFoo(Func<object> barImplementation)
    {
        Contract.Requires(barImplementation != null);
        _barImplementation = barImplementation;
    }
    private readonly Func<object> _barImplementation;

    [ContractInvariantMethod]
    private void ObjectInvariants()
    {
        Contract.Invariant(_barImplementation != null);
    }

    public object Bar()
    {
        var result = _barImplementation();
        Contract.Assume(result != null);
        return result;
    }
}
EN

Stack Overflow用户

发布于 2016-02-21 14:15:09

那么,您会一直控制传入的Func<object>实例吗?如果是这样,则Func<object>参数引用的每个函数都需要定义一个Contract.Ensures(Contract.Result<object>() != null)后置条件。

相反,如果您有一个Action参数,情况也是如此。

而且,我绝对不会使用Contract.Assume(result != null)。你确定你永远不会返回null吗?如果你这样做了,会发生什么?尝试将其更改为Contract.Assert(result != null)。如果您可以控制Func<object>参数并使用后置条件,那么您还应该能够在public object Bar()方法上指定一个后置条件,而不需要任何断言或假设。

最后,您可能需要考虑将Contract.Ensures(_barImplementation == barImplementation)添加到DelegatedFoo的构造函数中,不过,我认为后置条件已包含在已定义的不变量中。我总是喜欢直截了当--这肯定不会有什么坏处,特别是在静态分析器方面。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22238733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档