VS2008中应用.NET 4.0中的代码契约
.NET 4.0里引入了契约式编程
在VS2008中,可以下载组件,实现代码契约
CodeContract Tools 组件下载地址:http://research.microsoft.com/en-us/downloads/4ed7dd5f-490b-489e-8ca8-109324279968/default.aspx
安装后,打开VS2008,在项目中引入

编写测试代码如下:

运行测试代码,会看到效果。
在VS2008中,可以下载组件,实现代码契约
CodeContract Tools 组件下载地址:http://research.microsoft.com/en-us/downloads/4ed7dd5f-490b-489e-8ca8-109324279968/default.aspx
安装后,打开VS2008,在项目中引入

编写测试代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
namespace Client_Demo
{
class Program
{
static void Main(string[] args)
{
Test test = new Test(null);
Console.ReadLine();
}
}
public class Test
{
private object _obj;
public Test(object obj)
{
Contract.Requires(obj != null, "参数不能为 null
.");
_obj = obj;
}
}
}
然后在项目的属性中进行如下设置:using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
namespace Client_Demo
{
class Program
{
static void Main(string[] args)
{
Test test = new Test(null);
Console.ReadLine();
}
}
public class Test
{
private object _obj;
public Test(object obj)
{
Contract.Requires(obj != null, "参数不能为 null

_obj = obj;
}
}
}

运行测试代码,会看到效果。
KidYang