命名空间 : using System.Diagnostics.Contracts;

属性标记 : [ContractOption(category: "runtime", setting: "checking", enabled: true)]

事件订阅 : Contract.ContractFailed += (sender, e) => {  Console.WriteLine(e.Message);  };

1、 Requires() 定义前提条件

 static void MinMax(int min,int max)
{
Contract.Requires(min <= max);
Contract.Requires <ArgumentException>(min <= max);
}
static void Preconditions(object o)
{
Contract.Requires<ArgumentNullException>(o != null, "Preconditions, o may not be null");
Console.WriteLine(o.GetType().Name);
}
static void ArrayTest(int [] data)
{
Contract.Requires(Contract.ForAll(data, i => i < ));
Console.WriteLine("ArrayTest contract succeeded");
}
public void ArrayTestWithPureMethod(int [] data)
{
Contract.Requires(Contract.ForAll(data, MyDataCheck));
Console.WriteLine("ArrayWithPureMethod succeeded");
} public int MaxVal { get; set; }
public bool MyDataCheck(int x)
{
return x <= MaxVal;
}

2、  Ensures() 定义后置条件

   private static int sharedState = ;

         static void Postcondition()
{
Contract.Ensures(sharedState < );
sharedState = ;
Console.WriteLine("change sharedState invariant {0}", sharedState);
sharedState = ;
Console.WriteLine("before returning change it to a valid value {0}", sharedState);
} static int ReturnValue()
{
Contract.Ensures(Contract.Result<int>() < );
return ;
}
static int ReturnLargerThanInput(int x)
{
Contract.Ensures(Contract.Result<int>() > Contract.OldValue<int>(x));
return x+;
}
static void OutParameters(out int x, out int y)
{
Contract.Ensures(Contract.ValueAtReturn<int>(out x) > && Contract.ValueAtReturn<int>(out x) < );
Contract.Ensures(Contract.ValueAtReturn<int>(out y) % == );
x = ;
y = ;
}

3、  Invariant() 定义在对象的整个生命周期中都必须满足的条件

   private int x = ;
[ContractInvariantMethod]
private void ObjectInvariant()
{
Contract.Invariant(x > );
} public void Invariant()
{
x = ;
Console.WriteLine("invariant value: {0}", x);
}

4、  Pure特性,可以把方法和类型标记为纯粹的方法,纯粹指的是自定义方法不会修改对象的任何可见状态。

5、  接口协定

 [ContractClass(typeof(PersonContract))]
public interface IPerson
{
string FirstName{get;set;}
string LastName { get; set; }
int Age { get; set; }
void ChangeName(string firstName, string lastName);
} [ContractClassFor(typeof(IPerson))]
public abstract class PersonContract:IPerson
{
string IPerson.FirstName
{
get
{
return Contract.Result<string>();
}
set
{
Contract.Requires(value != null);
}
} string IPerson.LastName
{
get
{
return Contract.Result<string>();
}
set
{
Contract.Requires(value != null);
}
} int IPerson.Age
{
get
{
Contract.Ensures(Contract.Result<int>() >= && Contract.Result<int>() < );
return Contract.Result<int>();
}
set
{
Contract.Requires(value >= && value < );
}
} void IPerson.ChangeName(string firstName, string lastName)
{
Contract.Requires(firstName != null);
Contract.Requires(lastName != null);
}
} public class Person:IPerson
{
public Person() { }
public Person(string firstName,string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}
public string FirstName
{
get ;
set; } public string LastName
{
get;
set;
} public int Age
{
get;
set;
} public void ChangeName(string firstName, string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}
}

C# Contract诊断的更多相关文章

  1. 消费者驱动的契约Consumer drivern Contract

    消费者驱动的契约Consumer Driven Contracts (CDC) A contract between a consuming service and a providing servi ...

  2. 《Design by Contract for Embedded Software》 翻译

    原文: Design by Contract for Embedded Software (state-machine.com) Design by Contract is the single mo ...

  3. 利用Oracle RUEI+EM12c进行应用的“端到端”性能诊断

    概述 我们知道,影响一个B/S应用性能的因素,粗略地说,有以下几个大的环节: 1. 客户端环节 2. 网络环节(可能包括WAN和LAN) 3. 应用及中间层环节 4. 数据库层环节 能够对各个环节的问 ...

  4. SQL SERVER全面优化-------Expert for SQL Server 诊断系列

    现在很多用户被数据库的慢的问题所困扰,又苦于花钱请一个专业的DBA成本太高.软件维护人员对数据库的了解又不是那么深入,所以导致问题迟迟不能解决,或只能暂时解决不能得到根治.开发人员解决数据问题基本又是 ...

  5. WCF学习之旅—基于Fault Contract 的异常处理(十八)

       WCF学习之旅—WCF中传统的异常处理(十六) WCF学习之旅—基于ServiceDebug的异常处理(十七) 三.基于Fault Contract 的异常处理 第二个示例是通过定制Servic ...

  6. Sql Server 内存相关计数器以及内存压力诊断

    在数据库服务器中,内存是数据库对外提供服务最重要的资源之一, 不仅仅是Sql Server,包括其他数据库,比如Oracle,MySQL等,都是一类非常喜欢内存的应用. 在Sql Server服务器中 ...

  7. [转]Oracle10g数据库自动诊断监视工具(ADDM)使用指南

    第一章 ADDM简介                 在Oracle9i及之前,DBA们已经拥有了很多很好用的性能分析工具,比如,tkprof.sql_trace.statspack.set even ...

  8. Expert 诊断优化系列------------------你的CPU高么?

    现在很多用户被数据库的慢的问题所困扰,又苦于花钱请一个专业的DBA成本太高.软件维护人员对数据库的了解又不是那么深入,所以导致问题迟迟不能解决,或只能暂时解决不能得到根治.开发人员解决数据问题基本又是 ...

  9. Expert 诊断优化系列------------------内存不够用么?

    现在很多用户被数据库的慢的问题所困扰,又苦于花钱请一个专业的DBA成本太高.软件维护人员对数据库的了解又不是那么深入,所以导致问题迟迟不能解决,或只能暂时解决不能得到根治.开发人员解决数据问题基本又是 ...

随机推荐

  1. activate-power-mode安装与设置

    Window-->activate-power-mode-->去掉combo/shake,其他三个全勾上,现在用起来就很爽了,赶紧体验吧.

  2. 设计Qt风格的C++API

    在奇趣(Trolltech),为了改进Qt的开发体验,我们做了大量的研究.这篇文章里,我打算分享一些我们的发现,以及一些我们在设计Qt4时用到的原则,并且展示如何把这些原则应用到你的代码里. 优秀AP ...

  3. hdu 2647 还是逆向拓扑

    Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...

  4. [Vue]避免 v-if 和 v-for 用在同一个元素上

    一般我们在两种常见的情况下会倾向于这样做: 情形1:为了过滤一个列表中的项目 (比如 v-for="user in users" v-if="user.isActive& ...

  5. 【SQL Server性能优化】删除大量数据的方法比较

    原文:[SQL Server性能优化]删除大量数据的方法比较 如果你要删除表中的大量数据,这个大量一般是指删除大于10%的记录,那么如何删除,效率才会比较高呢? 而如何删除才会对系统的影响相对较小呢? ...

  6. Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form ResumeForm needs updating.

    django 报错 django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fiel ...

  7. 货币转换B

    描述 人民币和美元是世界上通用的两种货币之一,写一个程序进行货币间币值转换,其中:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮ ...

  8. 如何自定义starter

    在springboot启动流程的系列文章中,我们看过了springboot的自动配置机制,本文将基于自动配置机制自定义一个自动配置的starter示例 正文 模块结构 首先,我们准备两个模块servi ...

  9. Plugin 免费CSS生成器CssCollector

    下载: 百度云 自己在做Web开发的时候,页面里会有很多样式类,一个个复制到样式表里总感觉很麻烦 网上也没有找到合适的工具,可以一键生成样式表 所以,干脆自己做一个咯~ 案例展示 花了一天时间,CSS ...

  10. Go 缓冲信道

    缓冲信道 语法结构:cap为容量 ch := make(chan type, cap) 缓冲信道支持len()和cap(). 只能向缓冲信道发送容量以内的数据. 只能接收缓冲信道长度以内的数据. 缓冲 ...