标签: code-contracts

可以从立即窗口调用扩展方法

我问这个问题,因为每当我尝试从Visual Studio 2010中的立即窗口调用扩展方法时,我都会收到以下错误:

System.Collections.Generic.IEnumerable'不包含'ToList'的定义,也没有扩展方法'ToList'接受类型'System.Collections.Generic.IEnumerable'的第一个参数(你是否缺少using指令或装配参考?)

如果立即窗口不支持扩展方法,那么为什么当我键入我的变量(类型IEnumerable<QueryFilter>)后跟一个点时,IntelliSense会列出所有扩展方法?

我在命令窗口中输入的内容没有任何问题,因为如果我将其复制并粘贴到我的代码文件中并运行,则可以正常工作.

使用Visual Studio 2012为同一解决方案做同样的事情工作正常.如果我切换回VS2010并且问题仍然存在.

c# linq code-contracts visual-studio

13
推荐指数
4
解决办法
4569
查看次数

在代码合同中使用Contract.ForAll

好的,我还有另一个Code Contracts问题.我有一个接口方法的合同,看起来像这样(为清楚起见省略了其他方法):

[ContractClassFor(typeof(IUnboundTagGroup))]
public abstract class ContractForIUnboundTagGroup : IUnboundTagGroup
{
    public IUnboundTagGroup[] GetAllGroups()
    {
        Contract.Ensures(Contract.Result<IUnboundTagGroup[]>() != null);
        Contract.Ensures(Contract.ForAll(Contract.Result<IUnboundTagGroup[]>(), g => g != null));

        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

我有代码消耗这样的接口:

    public void AddRequested(IUnboundTagGroup group)
    {
            foreach (IUnboundTagGroup subGroup in group.GetAllGroups())
            {
                AddRequested(subGroup);
            }
            //Other stuff omitted
    }
Run Code Online (Sandbox Code Playgroud)

AddRequested需要一个非空输入参数(它实现具有需要契约的接口),所以我得到了"需要得到证实:组= NULL"在亚错误被传递到AddRequested.我正确使用ForAll语法吗?如果是这样,并在求解根本不理解,有另一种方式来帮助求解承认合同或者我只需要使用时GetAllGroups()被调用的假设?

c# static-analysis code-contracts forall

12
推荐指数
1
解决办法
1708
查看次数

代码合同:为什么有些不变量不在课堂外考虑?

考虑这种不可变类型:

public class Settings
{
    public string Path { get; private set; }

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

    public Settings(string path)
    {
        Contract.Requires(path != null);
        Path = path;
    }
}
Run Code Online (Sandbox Code Playgroud)

这里要注意两件事:

  • 有一个合同不变量,确保Path财产永远不会null
  • 构造函数检查path参数值以遵守先前的合约不变量

此时,Setting实例永远不会拥有null Path属性.

现在,看看这种类型:

public class Program
{
    private readonly string _path;

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

    public Program(Settings settings)
    {
        Contract.Requires(settings != null);
        _path = settings.Path;
    } // <------ "CodeContracts: …
Run Code Online (Sandbox Code Playgroud)

.net c# design-by-contract static-analysis code-contracts

12
推荐指数
1
解决办法
3108
查看次数

用PHP编写合同编程

按合同编程是.NET的一个现代趋势,但是PHP中的代码契约的库/框架呢?您如何看待这种范例对PHP的适用性?

谷歌搜索"代码合同PHP"没有给我任何帮助.

注意:"按合同编写代码",我指的是按合同设计,因此它与.NET或PHP接口无关.

php design-patterns design-by-contract code-contracts

12
推荐指数
1
解决办法
7096
查看次数

检查.NET中的前提条件

我是"早期失败"策略的粉丝,并希望检查方法params是否具有正确的值.在Java中我会使用像Guava这样的东西:

checkArgument(count > 0, "must be positive: %s", count);
Run Code Online (Sandbox Code Playgroud)

.NET有类似的东西吗?

.net validation code-contracts preconditions

12
推荐指数
2
解决办法
2412
查看次数

如何使用代码合同进行单元测试

使用.NET 4.0代码合同进行TDD的最佳实践建议是什么?

我特别想到,我的问题是,鉴于TDD的一个要点是允许代码自我记录并且合同现在提供了文档的一部分,代码合同是否应该像其他代码一样进行测试?

.net code-contracts c#-4.0

12
推荐指数
1
解决办法
3775
查看次数

关于参数的PureAttribute的目的

我理解PureAttribute用于标记某些东西(类,方法,委托等)没有可见的变化,但我可以从以下定义中看出它可以应用于方法参数:

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Constructor|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Event|AttributeTargets.Parameter|AttributeTargets.Delegate, AllowMultiple = false, Inherited = true)]
public sealed class PureAttribute : Attribute
Run Code Online (Sandbox Code Playgroud)

应用于参数的此属性的用途是什么,如下所示:

public void SomeMethod([Pure]SomeClass theParameter)
{
}
Run Code Online (Sandbox Code Playgroud)

是否暗示不SomeMethod应该使用theParameter未标记为的任何内容,这[Pure]意味着我们可以确保SomeClass在调用之前和之后看起来相同的实例是相同的SomeMethod

我没有看到PureAttribute这种方式的使用,并想知道这是由于代码合同缺乏支持还是因为对我的误解?

c# .net-4.0 code-contracts

12
推荐指数
1
解决办法
2162
查看次数

为什么静态分析会忽略double <=和> = require?

我有一个非常简单的类使用.NET代码合同:

public class ContractSquareRoot
{
    /// <summary>
    /// Makes your life much easier by calling Math.Sqrt for you. Ain't that peachy.
    /// </summary>
    /// <param name="value">The value to calculate the square root from. No negatives!</param>
    /// <returns>The square root of the given value. Obviously always > 0.</returns>
    public double CalculateSquareRoot(double value)
    {
        Contract.Requires<ArgumentException>(0 <= value);
        Contract.Ensures(0 <= Contract.Result<double>());

        double squareRoot = Math.Sqrt(value);

        return squareRoot;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我用负值调用方法时,我希望静态代码分析能够警告我.

class Program
{
    static void Main(string[] args)
    {

        var barMansSquareroot = new ContractSquareRoot(); …
Run Code Online (Sandbox Code Playgroud)

c# code-contracts

12
推荐指数
1
解决办法
315
查看次数

代码合同或if声明?

我只是尝试使用代码契约,而且我发现if语句没有真正的优势.

考虑以下.

private static void BindClassesToInterfacesByConvention(string classesEndingWith
    , string interfacesEndingwith) {
    Contract.Requires<ArgumentNullexception>(
        string.IsNullOrWhiteSpace(classesEndingWith)
        , "classesEndingWith");

    Contract.Requires<ArgumentNullException>(
        string.IsNullOrWhitespace(interfacesEndingWith)
        , "interfacesendingWith");

    ...
}
Run Code Online (Sandbox Code Playgroud)

我发现它比简单地使用一个更令人困惑 if statement

private static void BindClassesToInterfacesByConvention(string classesEndingWith
    , string interfacesEndingwith) {
    if (string.IsNullOrWhiteSpace(classesEndingWith)) 
        throw new ArgumentNullException("classesEndingWith");

    if (string.IsNullOrWhitespace(interfacesEndingWith))
        throw new ArgumentNullException("interfacesendingWith");

    ...
}
Run Code Online (Sandbox Code Playgroud)

代码合同应该在编译时警告我合同违反.因此,当我写下以下内容时,我期待得到错误或警告.

BindClassesToInterfacesByConvention(null, null);
Run Code Online (Sandbox Code Playgroud)

没有任何事情发生,一切编译得很好,既没有出现错误也没有出现警告信息.

在这种情况下,我认为最好继续使用it statement.或者也许是不公平的使用Code Contracts

c# if-statement code-contracts

11
推荐指数
1
解决办法
1372
查看次数

CodeContracts:布尔条件求值为常数值,为什么?

我收到了这个警告,但无法弄清楚问题......

CodeContracts:警告:布尔条件d1.Count!= d2.Count始终求值为常量值.如果它(或它的否定)出现在源代码中,您可能会有一些死代码或冗余检查

代码如下:

public static bool DictionaryEquals<TKey, TValue>(IDictionary<TKey, TValue> d1, IDictionary<TKey, TValue> d2)
{
    if (d1 == d2) return true;
    if (d1 == null || d2 == null) return false;
    if (d1.Count != d2.Count) return false; // <-- warning here

    // Equality check goes here

    return true;
}
Run Code Online (Sandbox Code Playgroud)

// Equality check goes here部分可以按原样,或由适当的实现替换,我仍然得到相同的警告.

c# code-contracts

11
推荐指数
1
解决办法
972
查看次数