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

编写测试代码如下:

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"参数不能为 nullVS2008中应用.NET 4.0中的代码契约.");
            _obj 
= obj;
        }
    }
}
然后在项目的属性中进行如下设置:
VS2008中应用.NET 4.0中的代码契约

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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2021-07-18
  • 2022-01-10
  • 2022-02-11
  • 2022-01-13
猜你喜欢
  • 2021-07-08
  • 2022-01-14
  • 2021-06-22
  • 2021-11-30
  • 2021-08-21
相关资源
相似解决方案