Release Validator

本文介绍了一个静态类Validator,该类在调试模式下提供多种断言检查和异常抛出方法,包括空引用、非法参数、无效操作及不支持的操作等。这些方法有助于开发者在开发阶段捕获潜在的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Rocky
{
    /// <summary>
    /// Release下会忽略此类所有方法
    /// </summary>
    [DebuggerStepThrough]
    public static class Validator
    {
        #region Fields
        public const string DebugSymbal = "DEBUG";
        #endregion

        #region Methods
        [Conditional(Validator.DebugSymbal)]
        public static void ThrowArgumentNullException(object arg, string paramName = null)
        {
            if (arg == null)
            {
                if (paramName == null)
                {
                    paramName = string.Empty;
                }
                Debug.Assert(false, paramName);
                throw new ArgumentNullException(paramName);
            }
        }

        [Conditional(Validator.DebugSymbal)]
        public static void ThrowArgumentException(bool isIllegal, string paramName = null)
        {
            if (isIllegal)
            {
                if (paramName == null)
                {
                    paramName = string.Empty;
                }
                Debug.Assert(false, paramName);
                throw new ArgumentException(string.Empty, paramName);
            }
        }

        [Conditional(Validator.DebugSymbal)]
        public static void ThrowInvalidOperationException(bool isIllegal, string message = null)
        {
            if (isIllegal)
            {
                if (message == null)
                {
                    message = string.Empty;
                }
                Debug.Assert(false, message);
                throw new InvalidOperationException(message);
            }
        }

        [Conditional(Validator.DebugSymbal)]
        public static void ThrowNotSupportedException(bool isIllegal, string message = null)
        {
            if (isIllegal)
            {
                if (message == null)
                {
                    message = string.Empty;
                }
                Debug.Assert(false, message);
                throw new NotSupportedException(message);
            }
        }

        [Conditional(Validator.DebugSymbal)]
        public static void ThrowException<T>(bool isIllegal, string environmentState) where T : Exception
        {
            if (isIllegal)
            {
                Debug.Assert(false, environmentState);
                try
                {
                    throw (T)Activator.CreateInstance(typeof(T), environmentState);
                }
                catch
                {
                    throw new InvalidOperationException(environmentState);
                }
            }
        }
        #endregion
    }
}

 

转载于:https://www.cnblogs.com/Googler/archive/2013/03/01/2939138.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值