C# Contract诊断
命名空间 : 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诊断的更多相关文章
- 消费者驱动的契约Consumer drivern Contract
消费者驱动的契约Consumer Driven Contracts (CDC) A contract between a consuming service and a providing servi ...
- 《Design by Contract for Embedded Software》 翻译
原文: Design by Contract for Embedded Software (state-machine.com) Design by Contract is the single mo ...
- 利用Oracle RUEI+EM12c进行应用的“端到端”性能诊断
概述 我们知道,影响一个B/S应用性能的因素,粗略地说,有以下几个大的环节: 1. 客户端环节 2. 网络环节(可能包括WAN和LAN) 3. 应用及中间层环节 4. 数据库层环节 能够对各个环节的问 ...
- SQL SERVER全面优化-------Expert for SQL Server 诊断系列
现在很多用户被数据库的慢的问题所困扰,又苦于花钱请一个专业的DBA成本太高.软件维护人员对数据库的了解又不是那么深入,所以导致问题迟迟不能解决,或只能暂时解决不能得到根治.开发人员解决数据问题基本又是 ...
- WCF学习之旅—基于Fault Contract 的异常处理(十八)
WCF学习之旅—WCF中传统的异常处理(十六) WCF学习之旅—基于ServiceDebug的异常处理(十七) 三.基于Fault Contract 的异常处理 第二个示例是通过定制Servic ...
- Sql Server 内存相关计数器以及内存压力诊断
在数据库服务器中,内存是数据库对外提供服务最重要的资源之一, 不仅仅是Sql Server,包括其他数据库,比如Oracle,MySQL等,都是一类非常喜欢内存的应用. 在Sql Server服务器中 ...
- [转]Oracle10g数据库自动诊断监视工具(ADDM)使用指南
第一章 ADDM简介 在Oracle9i及之前,DBA们已经拥有了很多很好用的性能分析工具,比如,tkprof.sql_trace.statspack.set even ...
- Expert 诊断优化系列------------------你的CPU高么?
现在很多用户被数据库的慢的问题所困扰,又苦于花钱请一个专业的DBA成本太高.软件维护人员对数据库的了解又不是那么深入,所以导致问题迟迟不能解决,或只能暂时解决不能得到根治.开发人员解决数据问题基本又是 ...
- Expert 诊断优化系列------------------内存不够用么?
现在很多用户被数据库的慢的问题所困扰,又苦于花钱请一个专业的DBA成本太高.软件维护人员对数据库的了解又不是那么深入,所以导致问题迟迟不能解决,或只能暂时解决不能得到根治.开发人员解决数据问题基本又是 ...
随机推荐
- airflow删除dag不在页面显示
当我们需要把dag删除的时候,遇到了删除了相应的dag文件,但页面还是显示 这个时候需要重启airflow 的webserver ps -ef|egrep rm -rf /home/airflow ...
- C++Primer 5th Chap7 Classes
this关键字: 在成员函数内部可以直接调用函数的对象的成员(类成员的直接访问看做是对this隐式引用,默认this指向非常量) 例如:string isbn() const{return this- ...
- js时间戳与日期格式之间相互转换
###js时间戳与日期格式之间相互转换 将时间戳转换成日期格式 // 简单的一句代码 var date = new Date(时间戳); //获取一个时间对象 /** 1. 下面是获取时间日期的方法, ...
- asp.net core-9.依赖注入的使用
http://www.jessetalk.cn/2017/11/06/di-in-aspnetcore/
- ant build打包
使用ant build进行增量打包 <?xml version="1.0" encoding="gb2312"?> <project name ...
- Apache2.4+Tomcat7.0整合配置详解
一.简单介绍 Apache.Tomcat Apache HTTP Server(简称 Apache),是 Apache 软件基金协会的一个开放源码的网页服务器,可以在 Windows.Unix.Lin ...
- Angular 惰性路由
根路由上的一个 loadChildren 属性,设置为一个字符串.这样就是惰性路由了. angular6 这样写:loadChildren: './background-check/backgroun ...
- 基因组所三代单分子测序PacBio完成技术升级—超长读长助力基因组学研究
基因组所三代单分子测序PacBio完成技术升级—超长读长助力基因组学研究 2015-09-23 | 作者:所级中心基因组平台 张兵 [关闭] 近日,基因组所所级中心基因组平台三代单分子实时测序PacB ...
- 国际化(i18n)学习
一 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 国际化(internationalization)又称 ...
- Tomcat服务器的数字证书 HTTPS 连接!
SUN公司提供了制作证书的工具keytool, 在JDK 1.4以后的版本中都包含了这一工具,它的位置为\bin\keytool.exe 注意要使用一下 cmd命令,请确认jdk环境变量可以使用,可以 ...