重复code合同要求下的产业链?产业链、合同、code

2023-09-07 15:26:59 作者:对你情深你却逗我。

假设你有一个现有的大项目,要集成code合同在里面。现有的code使用,如果空,然后掷逻辑。对于给定的条件下,文档建议设置汇编模式自定义参数验证。

我有以下类:

  A级
{
    受保护的虚拟无效美孚(INT A,INT B)
    {
        若(a == NULL)
            抛出新ArgumentNullException(一);
        如果(二== NULL)
            抛出新ArgumentNullException(B);
        Contract.EndContractBlock();
    }
}
B类:一
{
    保护覆盖无效美孚(INT A,INT B)
    {
        //一些东西
        base.Foo(A,B);
    }
}
 

当我编译,我得到以下警告:

  

警告CC1055:方法B.Foo(INT,INT)应包含自定义   参数验证要求(一!= NULL)'   因为它覆盖A.Foo(INT,INT),这意味着它的作用。如果不这样做   要使用自定义的参数验证在本届大会上,变更   汇编模式为标准合同要求。

我不想重复preconditions在每个覆盖的方法!有没有变通的办法?

解决方案

,如果你使用 Contract.Requires()而不是合同工作正常.EndContractBlock()

有低于该手册中的一个部分引用表明添加 [燮pressMessage] 属性的方法重写。

从 code合约用户手册第22页第5.2.3节。

  

委托检查,以其他方法

     

假设你有类似于以下code一code模式:

 公共类基地{
    公共虚拟无效计算(字符串数据){
    如果(数据== NULL)抛出新ArgumentNullException(...);
        Contract.EndContractBlock();
        ...
    }
}

公共类派生:基{
    公众覆盖无效计算(字符串数据){
        base.Compute(数据);
        ...
    }
}
 

     

然后在工具会发出警告,CC1055与格式的消息:

          

法Derived.Compute应包含自定义的参数验证     需要(ArgumentNullException)(数据!= NULL)',因为它覆盖     Base.Compute这表明它的作用。

        预见2021 2021年中国智慧消防行业全景图谱 附市场规模 竞争格局 发展前景等

在这种情况下,警告是没有帮助的,作为实现   Derived.Compute代表的参数验证到另一个   方法(在这种情况下,基法)。为了避免在此警告   无需重复验证的情况下,你可以添加一个   燮pressMessage属性的方法:

 公共类派生:基{
    【超级pressMessage(Microsoft.Contracts,CC1055,JUSTI科幻阳离子=验证中的基本方法执行)
    公众覆盖无效计算(字符串数据){
        base.Compute(数据);
        ...
    }
}
 

Suppose you have an existing large project and you want to integrate Code Contracts in it. The existing code uses if-null-then-throw logic. For the given conditions, the documentation suggests to set the assembly mode to Custom Argument Validation.

I have the following classes:

class A
{
    protected virtual void Foo(int a, int b)
    {
        if (a == null)
            throw new ArgumentNullException(a);
        if (b == null)
            throw new ArgumentNullException(b);
        Contract.EndContractBlock();
    }
}
class B : A
{
    protected override void Foo (int a, int b)
    {
        // some stuff
        base.Foo(a, b);
    }
}

When I compile I get the following warning:

warning CC1055: Method 'B.Foo(int, int)' should contain custom argument validation for 'Requires(a != null)' as it overrides 'A.Foo(int,int)' which suggests it does. If you don't want to use custom argument validation in this assembly, change the assembly mode to 'Standard Contract Requires'.

I don't want to repeat the preconditions on every overridden method! Is there a way around it?

解决方案

It works fine if you use Contract.Requires() instead of Contract.EndContractBlock().

There is a section in the manual quoted below which suggests adding a [SuppressMessage] attribute to the method override.

From the Code Contracts user manual p.22 section 5.2.3.

Delegating Checks to Other Methods

Suppose you have a code pattern similar to the following code:

public class Base {
    public virtual void Compute(string data) {
    if (data == null) throw new ArgumentNullException(...);
        Contract.EndContractBlock();
        ...
    }
}

public class Derived : Base {
    public override void Compute(string data) {
        base.Compute(data);
        ...
    }
}

Then the tools will issue warning CC1055 with a message of the form:

Method ’Derived.Compute’ should contain custom argument validation for ' Requires (ArgumentNullException)(data ! = null)' as it overrides 'Base.Compute' which suggests it does.

In this situation, the warning is not helpful, as the implementation of Derived.Compute delegates the parameter validation to another method (in this case the base method). To avoid the warning in this situation without repeating the validation, you can add a SuppressMessage attribute to the method:

public class Derived : Base {
    [SuppressMessage("Microsoft.Contracts", "CC1055", Justification = "Validation performed in base method")]
    public override void Compute(string data) {
        base.Compute(data);
        ...
    }
}