RaUI/Source/ryControls/HtmlRenderer/Core/Utils/ArgChecker.cs
zilinsoft 3262955f2f ### 2023-11-07更新
------
#### RaUIV4    V4.0.2311.0701
- *.[全新]整合了MyDb、ryControls、MyDb_MySQL等dll文件到RaUI一个项目。
- *.[新增]新增ApkOp类,可以轻松获取APK信息。
- *.[新增]新增JsonExt扩展类,让Json操作更简单。
- *.[新增]新增WebP类,可以支持webp格式的图片。
- *.[改进]ryQuickSQL中的AddField方法改为自动替换已存在的同名值。
- *.[修复]ryQuickSQL中的AddFieldCalc方法无法正常计算的BUG。
2023-11-07 16:37:53 +08:00

117 lines
4.5 KiB
C#

// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
using System;
using System.IO;
namespace TheArtOfDev.HtmlRenderer.Core.Utils
{
/// <summary>
/// Static class that contains argument-checking methods
/// </summary>
public static class ArgChecker
{
/// <summary>
/// Validate given condition is true, otherwise throw exception.
/// </summary>
/// <typeparam name="TException">Exception type to throw.</typeparam>
/// <param name="condition">Condition to assert.</param>
/// <param name="message">Exception message in-case of assert failure.</param>
public static void AssertIsTrue<TException>(bool condition, string message) where TException : Exception, new()
{
// Checks whether the condition is false
if (!condition)
{
// Throwing exception
throw (TException)Activator.CreateInstance(typeof(TException), message);
}
}
/// <summary>
/// Validate given argument isn't Null.
/// </summary>
/// <param name="arg">argument to validate</param>
/// <param name="argName">Name of the argument checked</param>
/// <exception cref="System.ArgumentNullException">if <paramref name="arg"/> is Null</exception>
public static void AssertArgNotNull(object arg, string argName)
{
if (arg == null)
{
throw new ArgumentNullException(argName);
}
}
/// <summary>
/// Validate given argument isn't <see cref="System.IntPtr.Zero"/>.
/// </summary>
/// <param name="arg">argument to validate</param>
/// <param name="argName">Name of the argument checked</param>
/// <exception cref="System.ArgumentNullException">if <paramref name="arg"/> is <see cref="System.IntPtr.Zero"/></exception>
public static void AssertArgNotNull(IntPtr arg, string argName)
{
if (arg == IntPtr.Zero)
{
throw new ArgumentException("IntPtr argument cannot be Zero", argName);
}
}
/// <summary>
/// Validate given argument isn't Null or empty.
/// </summary>
/// <param name="arg">argument to validate</param>
/// <param name="argName">Name of the argument checked</param>
/// <exception cref="System.ArgumentNullException">if <paramref name="arg"/> is Null or empty</exception>
public static void AssertArgNotNullOrEmpty(string arg, string argName)
{
if (string.IsNullOrEmpty(arg))
{
throw new ArgumentNullException(argName);
}
}
/// <summary>
/// Validate given argument isn't Null.
/// </summary>
/// <typeparam name="T">Type expected of arg</typeparam>
/// <param name="arg">argument to validate</param>
/// <param name="argName">Name of the argument checked</param>
/// <exception cref="System.ArgumentNullException">if <paramref name="arg"/> is Null</exception>
/// <returns>arg cast as T</returns>
public static T AssertArgOfType<T>(object arg, string argName)
{
AssertArgNotNull(arg, argName);
if (arg is T)
{
return (T)arg;
}
throw new ArgumentException(string.Format("Given argument isn't of type '{0}'.", typeof(T).Name), argName);
}
/// <summary>
/// Validate given argument isn't Null or empty AND argument value is the path of existing file.
/// </summary>
/// <param name="arg">argument to validate</param>
/// <param name="argName">Name of the argument checked</param>
/// <exception cref="System.ArgumentNullException">if <paramref name="arg"/> is Null or empty</exception>
/// <exception cref="System.IO.FileNotFoundException">if arg file-path not exist</exception>
public static void AssertFileExist(string arg, string argName)
{
AssertArgNotNullOrEmpty(arg, argName);
if (false == File.Exists(arg))
{
throw new FileNotFoundException(string.Format("Given file in argument '{0}' not exist.", argName), arg);
}
}
}
}