|
/// <summary>
///
/// </summary>
public static class Requires
{
/// <summary>
/// 要求给定的参数不能是null
/// </summary>
/// <param name="parameter">参数</param>
/// <param name="parameterName">参数名称</param>
/// <exception cref="System.ArgumentNullException"/>
public static void NotNull(object parameter, string parameterName)
{
if (parameter == null)
throw new ArgumentNullException(parameterName);
}
/// <summary>
/// 要求给定的字符串不能是null或者是空
/// </summary>
/// <param name="parameter">字符串参数</param>
/// <param name="parameterName">参数名称</param>
/// <exception cref="System.ArgumentException"/>
public static void NotNullOrEmpty(string parameter, string parameterName)
{
if (string.IsNullOrEmpty(parameter))
throw new ArgumentException(
"Parameter {0} is null or empty".FormatWith(parameterName), parameterName);
}
/// <summary>
/// 要求给定的字符串不能是null、空字符串或者全是空白字符
/// </summary>
/// <param name="parameter">字符串参数</param>
/// <param name="parameterName">参数名称</param>
/// <exception cref="System.ArgumentException"/>
public static void NotNullOrWhiteSpace(string parameter, string parameterName)
{
if (string.IsNullOrWhiteSpace(parameter))
throw new ArgumentException(
"Parameter {0} is null or whitespace".FormatWith(parameterName), parameterName);
}
/// <summary>
/// 要求满足特定的条件
/// </summary>
/// <param name="criteria">条件</param>
/// <param name="parameterName">参数名称</param>
/// <param name="message">消息</param>
public static void Satisfy(Func<bool> criteria, string parameterName, string message)
{
if (criteria == null)
return;
if (!criteria())
throw new ArgumentException(message, parameterName);
}
}
|
|