121 lines
4.1 KiB
C#
121 lines
4.1 KiB
C#
namespace Kit.Helpers
|
|
{
|
|
using System;
|
|
using System.Diagnostics;
|
|
|
|
/// <summary>
|
|
/// a static class that checks if parameter is invalid and raises an exception in this case
|
|
/// </summary>
|
|
public static class Check
|
|
{
|
|
[DebuggerStepThrough]
|
|
public static void ArgumentIsNotNull(object argument, string name)
|
|
{
|
|
if (argument == null)
|
|
{
|
|
throw new ArgumentNullException(name);
|
|
}
|
|
}
|
|
|
|
[DebuggerStepThrough]
|
|
public static void ArgumentIsNotNull(object argument, string name, string message)
|
|
{
|
|
if (argument == null)
|
|
{
|
|
throw new ArgumentException(message, name);
|
|
}
|
|
}
|
|
|
|
[DebuggerStepThrough]
|
|
public static void IntegerArgumentPositive(Int32 value, string name)
|
|
{
|
|
if (value < 1) throw new ArgumentOutOfRangeException(name, value, string.Empty);
|
|
}
|
|
|
|
[DebuggerStepThrough]
|
|
public static void IntegerArgumentPositive(Int32? value, string name)
|
|
{
|
|
if (!value.HasValue) throw new ArgumentNullException(name);
|
|
if (value.Value < 1) throw new ArgumentOutOfRangeException(name, value.Value, string.Empty);
|
|
}
|
|
|
|
[DebuggerStepThrough]
|
|
public static void IntegerArgumentPositive(Int64 value, string name)
|
|
{
|
|
if (value < 1) throw new ArgumentOutOfRangeException(name, value, string.Empty);
|
|
}
|
|
|
|
[DebuggerStepThrough]
|
|
public static void IntegerArgumentPositive(Int64? value, string name)
|
|
{
|
|
if (!value.HasValue) throw new ArgumentNullException(name);
|
|
if (value.Value < 1) throw new ArgumentOutOfRangeException(name, value.Value, string.Empty);
|
|
}
|
|
|
|
[DebuggerStepThrough]
|
|
public static void StringArgumentIsNotNullOrEmpty(string str, string name)
|
|
{
|
|
if (string.IsNullOrEmpty(str))
|
|
throw new ArgumentNullException(name);
|
|
|
|
str = str.Trim();
|
|
if (string.IsNullOrEmpty(str))
|
|
throw new ArgumentNullException(name);
|
|
}
|
|
|
|
[DebuggerStepThrough]
|
|
public static void IsTrue(bool argument, string message)
|
|
{
|
|
if (argument == false)
|
|
{
|
|
throw new InvalidOperationException(message);
|
|
}
|
|
}
|
|
|
|
[DebuggerStepThrough]
|
|
public static void IsNotTrue(bool argument, string message)
|
|
{
|
|
if (argument == true)
|
|
{
|
|
throw new InvalidOperationException(message);
|
|
}
|
|
}
|
|
|
|
[DebuggerStepThrough]
|
|
public static void IsNotNull(object argument, string message)
|
|
{
|
|
if (argument == null)
|
|
{
|
|
throw new InvalidOperationException(message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Checks whether argument is of specified type
|
|
/// Throws InvalidOperationException if argument is of wrong type
|
|
/// </summary>
|
|
/// <param name="argument">the argument to be checked</param>
|
|
/// <param name="type">the Type</param>
|
|
/// <param name="message">Implied to contain a {0} placeholder within in order to be replaced with string representation of the <para>type</para> param </param>
|
|
[DebuggerStepThrough]
|
|
public static void ArgumentIsOfType(object argument, Type type, string message)
|
|
{
|
|
Check.ArgumentIsNotNull(argument, "argument");
|
|
Check.ArgumentIsNotNull(type, "type");
|
|
|
|
if (type != argument.GetType())
|
|
{
|
|
throw new InvalidOperationException(string.Format(message, type.ToString()));
|
|
}
|
|
}
|
|
[DebuggerStepThrough]
|
|
public static void StringArgumentIsNotNullOrEmpty(string str, string paramName, string message)
|
|
{
|
|
if (string.IsNullOrEmpty(str))
|
|
throw new ArgumentNullException(paramName: paramName, message: message);
|
|
|
|
str = str.Trim();
|
|
if (string.IsNullOrEmpty(str))
|
|
throw new ArgumentNullException(paramName: paramName, message: message);
|
|
}
|
|
}
|
|
} |