Assertor用于判断参数和抛出异常
2018-06-17 20:34:11来源:未知 阅读 ()
代码
/// <summary> 断言器,用于判断和抛出异常 /// </summary> static class Assertor { /// <summary> 如果value值是null 则抛出异常 /// </summary> /// <param name="value">参数值</param> /// <param name="name">参数名称</param> public static void AreNull(object value, string name) { if (value == null) { throw new ArgumentNullException(name, "参数不能为null"); } } /// <summary> 如果value字符串是null或string.Empty 则抛出异常 /// </summary> /// <param name="value">参数值</param> /// <param name="name">参数名称</param> public static void AreNullOrEmpty(string value, string name) { if (value == null || value.Length == 0) { throw new ArgumentNullException(name, "字符串不能为null或空"); } } /// <summary> 如果value字符串是 null、空还是仅由空白字符组成 则抛出异常 /// </summary> /// <param name="value">参数值</param> /// <param name="name">参数名称</param> public static void AreNullOrWhiteSpace(string value, string name) { if (string.IsNullOrWhiteSpace(value)) { throw new ArgumentNullException(name, "字符串不能为null或连续空白"); } } /// <summary> 如果value集合是null或者Count是0 则抛出异常 /// </summary> /// <param name="value">参数值</param> /// <param name="name">参数名称</param> public static void AreNullOrEmpty(ICollection value, string name) { if (value == null || value.Count == 0) { throw new ArgumentNullException(name, "集合不能为null且必须有元素"); } } /// <summary> 如果value值超过min~max 则抛出异常 /// </summary> /// <param name="value">参数值</param> /// <param name="name">参数名称</param> /// <param name="min">最小值</param> /// <param name="max">最大值</param> public static void AreInRange<T>(T value, string name, T min, T max) where T : IComparable<T> { if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0) { throw new ArgumentOutOfRangeException(name, value, "值不能大于" + max + "或小于" + min); } } /// <summary> 如果value值超过min~max 则抛出异常 /// </summary> /// <param name="value">参数值</param> /// <param name="name">参数名称</param> /// <param name="min">最小值</param> /// <param name="max">最大值</param> public static void AreInRange<T>(IComparable value, string name, T min, T max) where T : IComparable { if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0) { throw new ArgumentOutOfRangeException(name, value, "值不能大于" + max + "或小于" + min); } } /// <summary> 如果value不是T类型或其子类 则抛出异常 /// </summary> /// <param name="value">参数值</param> /// <param name="name">参数名称</param> public static void AreType<T>(object value, string name) { if (value is T == false) { throw new ArgumentOutOfRangeException(name, value + " 值不是指定的类型 '" + typeof(T) + "'"); } } /// <summary> 如果value不是Type类型或其子类 则抛出异常 /// </summary> /// <param name="type">限定类型</param> /// <param name="value">参数值</param> /// <param name="name">参数名称</param> public static void AreType(Type type, object value, string name) { if (type.IsInstanceOfType(value) == false) { throw new ArgumentOutOfRangeException(name, value + " 值不是指定的类型 '" + type + "'"); } } /// <summary> 如果value不是数字类型 则抛出异常 /// </summary> /// <param name="value">参数值</param> /// <param name="name">参数名称</param> public static void AreNumberType(object value, string name) { var conv = value as IConvertible; if (conv == null) { var code = (int)conv.GetTypeCode(); if (code < 5 || code > 15) { throw new ArgumentOutOfRangeException(name, value + "不是数字类型"); } } } /// <summary> 如果condition是true 则抛出异常 /// </summary> /// <param name="condition">判断条件</param> /// <param name="message">异常消息</param> public static void AreTrue<T>(bool condition, string message) where T : Exception, new() { if (condition) { var ex = new T(); var lit = Literacy.Cache(typeof(Exception), false); lit.Load.NonPublicField(); lit.Field["_message"].SetValue(ex, message); throw ex; } } /// <summary> 如果condition是false 则抛出异常 /// </summary> /// <param name="condition">判断条件</param> /// <param name="message">异常消息</param> public static void AreFalse<T>(bool condition, string message) where T : Exception, new() { AreTrue<T>(!condition, message); } /// <summary> 如果condition是true 则抛出NotSupportedException异常 /// </summary> /// <param name="condition">判断条件</param> /// <param name="message">异常消息</param> public static void AreTrue(bool condition, string message) { AreTrue<NotSupportedException>(condition, message); } /// <summary> 如果condition是false 则抛出NotSupportedException异常 /// </summary> /// <param name="condition">判断条件</param> /// <param name="message">异常消息</param> public static void AreFalse(bool condition, string message) { AreTrue<NotSupportedException>(!condition, message); } }
调用
private static SimpleCache<IComponentFactory> GetFactoryCache() { return new SimpleCache<IComponentFactory>(true, key => { IComponentFactory factory; var provider = _ProvideCache[key]; if (provider == DBProvider.Other) { var type = Type.GetType(key, false, false); Assertor.AreTrue<NotSupportedException>(type == null, "没有找到名为" + key + "的类型"); factory = Activator.CreateInstance(type) as IComponentFactory; Assertor.AreTrue<NotSupportedException>(factory == null, type.FullName + "无法实例化或没有继承IComponentFactory"); } else { factory = ComponentFactory.Create(provider); } return factory; }); }
CodeCSDN
https://code.csdn.net/snippets/389206
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Python之装饰器笔记 2019-08-13
- python变量和常量 2019-08-13
- python3 之 判断闰年小实例 2019-08-13
- 单例模式 2019-07-24
- 输入某年某月某日,判断这一天是这一年的第几天 2019-07-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash