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

2018-06-17 19:46:07来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

代码

/// <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

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:属性与字段的区别

下一篇:系统的方法就一定是最好的?