Assertor用于判断参数和抛出异常_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > Assertor用于判断参数和抛出异常

Assertor用于判断参数和抛出异常

 2014/6/12 16:11:10  冰麟轻武  程序员俱乐部  我要评论(0)
  • 摘要:代码///<summary>断言器,用于判断和抛出异常///</summary>staticclassAssertor{///<summary>如果value值是null则抛出异常///</summary>///<paramname="value">参数值</param>///<paramname="name">参数名称</param>publicstaticvoidAreNull
  • 标签:异常

代码

class="code_img_closed" src="/Upload/Images/2014061216/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('f89acad6-cfc6-46dc-a08b-372fa7aaea98',event)" src="/Upload/Images/2014061216/2B1B950FA3DF188F.gif" alt="" />
/// <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);
    }
}
View Code

调用

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

 

发表评论
用户名: 匿名