Assertor用于判断参数和抛出异常
代码
/// <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
我写的文章,除了纯代码,其他的都是想表达一种思想,一种解决方案.希望各位看官不要局限于文章中的现成的代码,要多关注整个文章的主题思路,谢谢!
我发布的代码,没有任何版权,遵守WTFPL协议(如有引用,请遵守被引用代码的协议)
qq群:5946699 希望各位喜爱C#的朋友可以在这里交流学习,分享编程的心得和快乐
我发布的代码,没有任何版权,遵守WTFPL协议(如有引用,请遵守被引用代码的协议)
qq群:5946699 希望各位喜爱C#的朋友可以在这里交流学习,分享编程的心得和快乐