C# 在Func上定义代码合同<&燃气轮机;

C# 在Func上定义代码合同<&燃气轮机;,c#,code-contracts,C#,Code Contracts,我正在尝试集成到一些现有的代码中,到目前为止,大部分代码都很好。但我面临的问题是,我在接口上设置了契约,一个实现将实现委托给Func,Func被传递到类的构造函数中,类似于以下内容: [ContractClass(typeof(IFooContract))] public interface IFoo { object Bar(); } [ContractClassFor(typeof(IFoo))] public abstract class IFooContract : IFoo

我正在尝试集成到一些现有的代码中,到目前为止,大部分代码都很好。但我面临的问题是,我在接口上设置了契约,一个实现将实现委托给Func,Func被传递到类的构造函数中,类似于以下内容:

[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();
    }
}
[ContractClass(typeof(IFooContract))]
公共接口IFoo
{
对象栏();
}
[ContractClassFor(typeof(IFoo))]
公共抽象类IFooContract:IFoo
{
对象IFoo.Bar()
{
Contract.sure(Contract.Result()!=null);
抛出新的NotImplementedException();
}
}
公共类DelegatedFoo:IFoo
{
公共委托OO(职能实施)
{
合同要求(b执行!=null);
_barImplementation=barImplementation;
}
私有只读功能的实现;
[收缩变量法]
私有无效对象变量()
{
Contract.Invariant(_barImplementation!=null);
}
公共对象栏()
{
//此处的“未经验证:Contract.Result()!=null”。
return_barImplementation();
}
}
静态分析器报告错误“确保未经验证:Contract.Result()!=null”。我可以在Func上定义合同(并通过扩展操作)吗

这是最好的解决方案吗

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;
    }
}
public类DelegatedFoo:IFoo
{
公共委托OO(职能实施)
{
合同要求(b执行!=null);
_barImplementation=barImplementation;
}
私有只读功能的实现;
[收缩变量法]
私有无效对象变量()
{
Contract.Invariant(_barImplementation!=null);
}
公共对象栏()
{
var result=_barImplementation();
合同。假设(结果!=null);
返回结果;
}
}
那么,您是否始终可以控制传入的
Func
实例?如果是这样,则
Func
参数引用的每个函数都需要定义一个
Contract.assures(Contract.Result()!=null)
post条件

如果您有一个
Action
参数,则情况也是如此

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

<>最后,您可能需要考虑添加<代码>合同。请确保(yBARTACKION==BARTActudio)< /C>到构造函数<代码>委托-Foo,但是,我假设后置条件被定义的不变量覆盖。我总是喜欢明确地说,这肯定不会有什么坏处,特别是在静态分析器方面