您的位置:首页 > 产品设计 > UI/UE

Code Contracts 关于方法Contract.Requires<T>()

2016-02-14 00:00 531 查看
摘要: Contract.Requires<T>()

class Program
{
static void Main(string[] args)
{
A a = new A();
try
{
a.PutA(Console.ReadLine());
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}

[ContractClass(typeof(AContract))]
public interface IA
{
int ErrorInt { get; set; }
void PutA(string str);
}

[ContractClassFor(typeof(IA))]
sealed class AContract:IA
{
public int ErrorInt { get; set; }

public void PutA(string str)
{
//Console.WriteLine(nameof(AContract));
Contract.Requires<ArgumentNullException>(!String.IsNullOrEmpty(str),"str");
//Contract.Requires(str != null);
//Contract.Ensures(str!=null);
//Contract.EnsuresOnThrow<ArgumentNullException>(str == null);
//Contract.Requires<ArgumentNullException>(ErrorInt == 0,nameof(ErrorInt));
}
}

在调用

a.PutA(Console.ReadLine());

输入为空的时候 不会抛异常,

然后查了资料

这个需要在项目属性理勾选 Call-site Requires Checking
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: