在C#中“契约式设计” [英] 'Design By Contract' in C#

查看:157
本文介绍了在C#中“契约式设计”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试通过合同一点点的设计在我最新的C#应用​​程序,并希望有类似的语法:

 公共字符串美孚()
{
    集合{
        Assert.IsNotNull(值);
        Assert.IsTrue(value.Contains(酒吧));
        _foo =价值;
    }
}

我知道我可以从一个单元测试框架这样的静态方法,但我想知道,如果这样的事情已经内置到语言,或者已经有某种框架左右浮动。我可以写我自己的断言功能,只是不想推倒重来。


解决方案

  

C#4.0 code合约


微软在.NET Framework 4.0版发布了设计库合同。一个该库的最酷的功能是,它也配备了一个静态分析工具(类似的FxCop我猜),它利用放置在code合同的细节。

下面是一些微软的资源:

下面是其他一些资源:

I wanted to try a little design by contract in my latest C# application and wanted to have syntax akin to:

public string Foo()
{
    set {
        Assert.IsNotNull(value);
        Assert.IsTrue(value.Contains("bar"));
        _foo = value;
    }
}

I know I can get static methods like this from a unit test framework, but I wanted to know if something like this was already built-in to the language or if there was already some kind of framework floating around. I can write my own Assert functions, just don't want to reinvent the wheel.

解决方案

C# 4.0 Code Contracts

Microsoft has released a library for design by contract in version 4.0 of the .net framework. One of the coolest features of that library is that it also comes with a static analysis tools (similar to FxCop I guess) that leverages the details of the contracts you place on the code.

Here are some Microsoft resources:

Here are some other resources:

这篇关于在C#中“契约式设计”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆