105 lines
2.4 KiB
C#
105 lines
2.4 KiB
C#
using DispenserCommon.Exceptions;
|
|
|
|
namespace DispenserCommon.Utils;
|
|
|
|
/// <summary>
|
|
/// 断言工具类
|
|
/// </summary>
|
|
public class AssertUtil
|
|
{
|
|
/// <summary>
|
|
/// 判断条件是否为真
|
|
/// </summary>
|
|
/// <param name="expression"></param>
|
|
/// <param name="msg"></param>
|
|
public static void IsTrue(bool expression, string msg)
|
|
{
|
|
if (!expression)
|
|
{
|
|
throw new AssertException(msg);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断条件是否为假
|
|
/// </summary>
|
|
/// <param name="expression"></param>
|
|
/// <param name="msg"></param>
|
|
public static void IsFalse(bool expression, string msg)
|
|
{
|
|
if (expression)
|
|
{
|
|
throw new AssertException(msg);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 集合非空断言
|
|
/// </summary>
|
|
/// <param name="list"></param>
|
|
/// <param name="msg"></param>
|
|
public static void NotEmpty(List<object>? list, string msg)
|
|
{
|
|
if (list == null || list.Count == 0)
|
|
{
|
|
throw new AssertException(msg);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 集合空断言
|
|
/// </summary>
|
|
/// <param name="list"></param>
|
|
/// <param name="msg"></param>
|
|
public static void IsEmpty(List<object> list, string msg)
|
|
{
|
|
if (list is { Count: > 0 })
|
|
{
|
|
throw new AssertException(msg);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 对象非空断言
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <param name="msg"></param>
|
|
/// <exception cref="AssertException"></exception>
|
|
public static void NotNull(object? model, string msg)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new AssertException(msg);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 对象空断言
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <param name="msg"></param>
|
|
/// <exception cref="AssertException"></exception>
|
|
public static void IsNull(object? model, string msg)
|
|
{
|
|
if (model != null)
|
|
{
|
|
throw new AssertException(msg);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 字符串非空断言
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <param name="msg"></param>
|
|
/// <exception cref="AssertException"></exception>
|
|
public static void StringNotNullOrEmpty(string str, string msg)
|
|
{
|
|
if (string.IsNullOrEmpty(str))
|
|
{
|
|
throw new AssertException(msg);
|
|
}
|
|
}
|
|
} |