Class Validator
- Namespace
- Cuemon
- Assembly
- Cuemon.Core.dll
Provides a generic way to validate different types of arguments passed to members.
public sealed class Validator
- Inheritance
-
ObjectValidator
Properties
ThrowIf
Gets the singleton instance of the Validator functionality allowing for extensions methods like: Validator.ThrowIf.InvalidJsonDocument()
.
public static Validator ThrowIf { get; }
Property Value
- Validator
The singleton instance of the Validator functionality.
Methods
CheckParameter<T>(T, Action)
Provides a convenient way to verify a desired state from the provided validator
while returning the specified argument
unaltered.
public static T CheckParameter<T>(T argument, Action validator)
Parameters
argument
TThe value to be evaluated.
validator
ActionThe delegate that must throw an System.Exception if the specified
argument
is not valid.
Returns
- T
The specified
argument
unaltered.
Type Parameters
T
The type of the object to evaluate.
Remarks
Typically used when nesting calls from a constructor perspective.
Exceptions
- ArgumentException
validator
cannot be null.
CheckParameter<TResult>(Func<TResult>)
Provides a convenient way to verify a desired state from the provided validator
while returning a result that reflects this.
public static TResult CheckParameter<TResult>(Func<TResult> validator)
Parameters
validator
Func<TResult>The function delegate that must throw an System.Exception if a desired state for
TResult
cannot be achieved.
Returns
- TResult
The result of function delegate
validator
.
Type Parameters
TResult
The type of the object to return.
Remarks
Typically used when nesting calls from a constructor perspective.
Exceptions
- ArgumentException
validator
cannot be null.
ThrowIfContainsInterface(Type, String, String, Type[])
Validates and throws an System.ArgumentOutOfRangeException if the specified argument
is contained within at least one of the specified types
.
public static void ThrowIfContainsInterface(Type argument, string paramName, string message, params Type[] types)
Parameters
argument
TypeThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
types
Type[]A variable number of System.Type arguments (that must be an interface) to match with the type of
argument
.
Exceptions
- System.ArgumentNullException
argument
is null -or-types
is null.- ArgumentOutOfRangeException
argument
is contained within at least one of the specifiedtypes
.- ArgumentException
types
does not satisfy the condition of being an interface.
ThrowIfContainsInterface(Type, String, Type[])
Validates and throws an System.ArgumentOutOfRangeException if the specified argument
is contained within at least one of the specified types
.
public static void ThrowIfContainsInterface(Type argument, string paramName, params Type[] types)
Parameters
argument
TypeThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
types
Type[]A variable number of System.Type arguments (that must be an interface) to match with the type of
argument
.
Exceptions
- System.ArgumentNullException
argument
is null - or -types
is null.- ArgumentOutOfRangeException
argument
is contained within at least one of the specifiedtypes
.- ArgumentException
types
does not satisfy the condition of being an interface.
ThrowIfContainsInterface<T>(String, String, Type[])
Validates and throws an TypeArgumentOutOfRangeException if the specified T
is contained within at least one of the specified types
.
public static void ThrowIfContainsInterface<T>(string typeParamName, string message, params Type[] types)
Parameters
typeParamName
StringThe name of the type parameter that caused the exception.
message
StringA message that describes the error.
types
Type[]A variable number of System.Type arguments (that must be an interface) to match with the type of
T
.
Type Parameters
T
Exceptions
- System.ArgumentNullException
types
is null.- TypeArgumentOutOfRangeException
T
is contained within at least one of the specifiedtypes
.- ArgumentException
types
does not satisfy the condition of being an interface.
ThrowIfContainsInterface<T>(String, Type[])
Validates and throws a TypeArgumentOutOfRangeException if the specified T
is contained within at least one of the specified types
.
public static void ThrowIfContainsInterface<T>(string typeParamName, params Type[] types)
Parameters
typeParamName
StringThe name of the type parameter that caused the exception.
types
Type[]A variable number of System.Type arguments (that must be an interface) to match with the type of
T
.
Type Parameters
T
Exceptions
- System.ArgumentNullException
types
is null.- TypeArgumentOutOfRangeException
T
is contained within at least one of the specifiedtypes
.- ArgumentException
types
does not satisfy the condition of being an interface.
ThrowIfContainsType(Object, String, String, Type[])
Validates and throws an System.ArgumentOutOfRangeException if the specified argument
is contained within at least one of the specified types
.
public static void ThrowIfContainsType(object argument, string paramName, string message, params Type[] types)
Parameters
argument
ObjectThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
types
Type[]A variable number of System.Type arguments to match with the type of
argument
.
Exceptions
- System.ArgumentNullException
argument
is null - or -types
is null.- ArgumentOutOfRangeException
argument
is contained within at least one of the specifiedtypes
.
ThrowIfContainsType(Object, String, Type[])
Validates and throws an System.ArgumentOutOfRangeException if the specified argument
is contained within at least one of the specified types
.
public static void ThrowIfContainsType(object argument, string paramName, params Type[] types)
Parameters
argument
ObjectThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
types
Type[]A variable number of System.Type arguments to match with the type of
argument
.
Exceptions
- System.ArgumentNullException
argument
is null - or -types
is null.- ArgumentOutOfRangeException
argument
is contained within at least one of the specifiedtypes
.
ThrowIfContainsType(Type, String, String, Type[])
Validates and throws an System.ArgumentOutOfRangeException if the specified argument
is contained within at least one of the specified types
.
public static void ThrowIfContainsType(Type argument, string paramName, string message, params Type[] types)
Parameters
argument
TypeThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
types
Type[]A variable number of System.Type arguments to match with the type of
argument
.
Exceptions
- System.ArgumentNullException
argument
is null - or -types
is null.- ArgumentOutOfRangeException
argument
is contained within at least one of the specifiedtypes
.
ThrowIfContainsType(Type, String, Type[])
Validates and throws an System.ArgumentOutOfRangeException if the specified argument
is contained within at least one of the specified types
.
public static void ThrowIfContainsType(Type argument, string paramName, params Type[] types)
Parameters
argument
TypeThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
types
Type[]A variable number of System.Type arguments to match with the type of
argument
.
Exceptions
- System.ArgumentNullException
argument
is null - or -types
is null.- ArgumentOutOfRangeException
argument
is contained within at least one of the specifiedtypes
.
ThrowIfContainsType<T>(String, String, Type[])
Validates and throws a TypeArgumentOutOfRangeException if the specified T
is contained within at least one of the specified types
.
public static void ThrowIfContainsType<T>(string typeParamName, string message, params Type[] types)
Parameters
typeParamName
StringThe name of the type parameter that caused the exception.
message
StringA message that describes the error.
types
Type[]A variable number of System.Type arguments to match with the type of
T
.
Type Parameters
T
Exceptions
- System.ArgumentNullException
types
is null.- TypeArgumentOutOfRangeException
T
is contained within at least one of the specifiedtypes
.
ThrowIfContainsType<T>(String, Type[])
Validates and throws a TypeArgumentOutOfRangeException if the specified T
is contained within at least one of the specified types
.
public static void ThrowIfContainsType<T>(string typeParamName, params Type[] types)
Parameters
typeParamName
StringThe name of the type parameter that caused the exception.
types
Type[]A variable number of System.Type arguments to match with the type of
T
.
Type Parameters
T
Exceptions
- System.ArgumentNullException
types
is null.- TypeArgumentOutOfRangeException
T
is contained within at least one of the specifiedtypes
.
ThrowIfDisposed(Boolean, Object, String)
Validates and throws an System.ObjectDisposedException if the specified condition
is true
.
public static void ThrowIfDisposed(bool condition, object instance, string message = "Cannot access a disposed object.")
Parameters
condition
BooleanThe condition to evaluate.
instance
ObjectThe object whose type's full name should be included in any resulting System.ObjectDisposedException.
message
StringA message that describes the error.
Remarks
This guard should be called when performing an operation on a disposed object - not when validating arguments passed to a member.
Exceptions
- System.ObjectDisposedException
The
condition
istrue
.
ThrowIfDisposed(Boolean, Type, String)
Validates and throws an System.ObjectDisposedException if the specified condition
is true
.
public static void ThrowIfDisposed(bool condition, Type type, string message = "Cannot access a disposed object.")
Parameters
condition
BooleanThe condition to evaluate.
type
TypeThe type whose full name should be included in any resulting System.ObjectDisposedException.
message
StringA message that describes the error.
Remarks
This guard should be called when performing an operation on a disposed object - not when validating arguments passed to a member.
Exceptions
- System.ObjectDisposedException
The
condition
istrue
.
ThrowIfEmailAddress(String, String, String)
Validates and throws an System.ArgumentException if the specified argument
has the format of an email address.
public static void ThrowIfEmailAddress(string argument, string paramName = null, string message = "Value cannot be an email address.")
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Exceptions
- ArgumentException
argument
cannot be an email address.
ThrowIfEmpty(String, String, String)
Validates and throws an System.ArgumentException if the specified argument
is empty.
public static void ThrowIfEmpty(string argument, string paramName, string message = "Value cannot be empty.")
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Remarks
This method will not throw an exception if argument
is null.
Exceptions
- ArgumentException
argument
cannot be empty.
ThrowIfEnum<TEnum>(String, String, Boolean, String)
Validates and throws an System.ArgumentException if the specified argument
represents an enumeration.
public static void ThrowIfEnum<TEnum>(string argument, string paramName = null, bool ignoreCase = true, string message = "Value represents an enumeration.")
where TEnum : struct, IConvertible
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
ignoreCase
Booleantrue
to ignore case;false
to regard case.message
StringA message that describes the error.
Type Parameters
TEnum
The type of the enumeration.
Exceptions
- ArgumentException
argument
represents an enumeration.
ThrowIfEnumType(Type, String, String)
Validates and throws an System.ArgumentException if the specified argument
represents an enumeration.
public static void ThrowIfEnumType(Type argument, string paramName = null, string message = "Value represents an enumeration.")
Parameters
argument
TypeThe type to check is an enumeration.
paramName
StringThe name of the type parameter that caused the exception.
message
StringA message that describes the error.
Remarks
This method will not throw an exception if argument
is null.
Exceptions
- ArgumentException
argument
represents an enumeration.
ThrowIfEnumType<TEnum>(String, String)
Validates and throws a TypeArgumentException if the specified TEnum
represents an enumeration.
public static void ThrowIfEnumType<TEnum>(string typeParamName, string message = "Value represents an enumeration.")
Parameters
typeParamName
StringThe name of the type parameter that caused the exception.
message
StringA message that describes the error.
Type Parameters
TEnum
The type to check is an enumeration.
Exceptions
- TypeArgumentException
TEnum
represents an enumeration.
ThrowIfEqual<T>(T, T, String, IEqualityComparer<T>, String)
Validates and throws an System.ArgumentOutOfRangeException if the specified x
object are equal to the y
object.
public static void ThrowIfEqual<T>(T x, T y, string paramName, IEqualityComparer<T> comparer = null, string message = null)
Parameters
x
TThe first object to compare.
y
TThe second object to compare.
paramName
StringThe name of the parameter that caused the exception.
comparer
IEqualityComparer<T>The System.Collections.Generic.IEqualityComparer<T> implementation to use when comparing
x
andy
.message
StringA message that describes the error.
Type Parameters
T
The type of objects to compare.
Exceptions
- ArgumentOutOfRangeException
x
andy
are equal to one another.
ThrowIfFalse(Boolean, String, String, String)
Validates and throws an System.ArgumentException if the specified condition
is false
.
public static void ThrowIfFalse(bool condition, string paramName, string message = "Value is not in a valid state.", string expression = null)
Parameters
condition
BooleanThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
expression
StringThe
condition
expressed as a string.
Exceptions
- ArgumentException
condition
isfalse
.
ThrowIfFalse(Func<Boolean>, String, String, String)
Validates and throws an System.ArgumentException if the specified predicate
returns false
.
public static void ThrowIfFalse(Func<bool> predicate, string paramName, string message = "Value is not in a valid state.", string expression = null)
Parameters
predicate
Func<Boolean>The function delegate that determines if an System.ArgumentException is thrown.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
expression
StringThe
predicate
expressed as a string.
Exceptions
- ArgumentException
predicate
returnedfalse
.
ThrowIfGreaterThan<T>(T, T, String, String)
Validates and throws an System.ArgumentOutOfRangeException if the specified x
is greater than y
.
public static void ThrowIfGreaterThan<T>(T x, T y, string paramName, string message = null)
where T : struct, IConvertible
Parameters
x
TThe first object to compare.
y
TThe second object to compare.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Type Parameters
T
The type of objects to compare.
Exceptions
- ArgumentOutOfRangeException
x
is greater thany
.
ThrowIfGreaterThanOrEqual<T>(T, T, String, String)
Validates and throws an System.ArgumentOutOfRangeException if the specified x
is greater than or equal to y
.
public static void ThrowIfGreaterThanOrEqual<T>(T x, T y, string paramName, string message = null)
where T : struct, IConvertible
Parameters
x
TThe first object to compare.
y
TThe second object to compare.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Type Parameters
T
The type of objects to compare.
Exceptions
- ArgumentOutOfRangeException
x
is greater than or equal toy
.
ThrowIfGuid(String, String, GuidFormats, String)
Validates and throws an System.ArgumentException if the specified argument
has the format of a System.Guid.
public static void ThrowIfGuid(string argument, string paramName = null, GuidFormats format = GuidFormats.D | GuidFormats.B | GuidFormats.P, string message = "Value cannot be a Guid.")
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
format
GuidFormatsA bitmask comprised of one or more GuidFormats that specify how the GUID parsing is conducted.
message
StringA message that describes the error.
Exceptions
- ArgumentException
argument
cannot be a System.Guid.
ThrowIfHex(String, String, String)
Validates and throws an System.ArgumentException if the specified argument
is hexadecimal.
public static void ThrowIfHex(string argument, string paramName = null, string message = "Specified argument cannot be hexadecimal.")
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Exceptions
- ArgumentException
argument
cannot be hexadecimal.
ThrowIfInvalidConfigurator<TOptions>(Action<TOptions>, out TOptions, String, String)
Validates and throws an System.ArgumentException if the specified argument
results in an instance of invalid options
.
public static void ThrowIfInvalidConfigurator<TOptions>(Action<TOptions> argument, out TOptions options, string paramName = null, string message = "Delegate must configure the public read-write properties to be in a valid state.")
where TOptions : class, IParameterObject, new()
Parameters
argument
Action<TOptions>The delegate that will configure the public read-write properties of
options
.options
TOptionsThe default parameter-less constructed instance of
TOptions
configured withargument
delegate.paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Type Parameters
TOptions
The type of the object that potentially is implementing the IValidatableParameterObject interface.
Exceptions
- ArgumentException
argument
failed to configure an instanceoptions
in a valid state.
ThrowIfInvalidOptions<TOptions>(TOptions, String, String)
Validates and throws an System.ArgumentException if the specified argument
are not in a valid state.
public static void ThrowIfInvalidOptions<TOptions>(TOptions argument, string paramName = null, string message = "{0} are not in a valid state.")
where TOptions : class, IParameterObject, new()
Parameters
argument
TOptionsThe configured options to validate.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Type Parameters
TOptions
The type of the object that potentially is implementing the IValidatableParameterObject interface.
Remarks
message
will have the name of the TOptions
if possible; otherwise Options.
Exceptions
- System.ArgumentNullException
argument
cannot be null.- ArgumentException
argument
are not in a valid state.
ThrowIfInvalidState(Boolean, String, String)
Validates and throws an System.InvalidOperationException if the specified condition
is true
.
public static void ThrowIfInvalidState(bool condition, string message = "Operation is not valid due to the current state of the object.", string expression = null)
Parameters
condition
BooleanThe value that determines if an System.InvalidOperationException is thrown.
message
StringA message that describes the error.
expression
StringThe
condition
expressed as a string.
Remarks
This guard should be called when validating the state of an object - not when validating arguments passed to a member.
Exceptions
- System.InvalidOperationException
condition
istrue
.
ThrowIfLowerThan<T>(T, T, String, String)
Validates and throws an System.ArgumentOutOfRangeException if the specified x
is lower than y
.
public static void ThrowIfLowerThan<T>(T x, T y, string paramName, string message = null)
where T : struct, IConvertible
Parameters
x
TThe first object to compare.
y
TThe second object to compare.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Type Parameters
T
The type of objects to compare.
Exceptions
- ArgumentOutOfRangeException
x
is lower thany
.
ThrowIfLowerThanOrEqual<T>(T, T, String, String)
Validates and throws an System.ArgumentOutOfRangeException if the specified x
is lower than or equal to y
.
public static void ThrowIfLowerThanOrEqual<T>(T x, T y, string paramName, string message = null)
where T : struct, IConvertible
Parameters
x
TThe first object to compare.
y
TThe second object to compare.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Type Parameters
T
The type of objects to compare.
Exceptions
- ArgumentOutOfRangeException
x
is lower than or equal toy
.
ThrowIfNotBase64String(String, String, String)
Validates and throws an System.ArgumentOutOfRangeException if the specified argument
consist of anything besides a base-64 structure.
public static void ThrowIfNotBase64String(string argument, string paramName = null, string message = "Value must consist only of base-64 digits.")
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Exceptions
- ArgumentOutOfRangeException
argument
must consist only of base-64 digits.
ThrowIfNotBinaryDigits(String, String, String)
Validates and throws an System.ArgumentOutOfRangeException if the specified argument
consist of anything besides binary digits.
public static void ThrowIfNotBinaryDigits(string argument, string paramName = null, string message = "Value must consist only of binary digits.")
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Exceptions
- ArgumentOutOfRangeException
argument
must consist only of binary digits.
ThrowIfNotContainsInterface(Type, String, String, Type[])
Validates and throws an System.ArgumentOutOfRangeException if the specified argument
is not contained within at least one of the specified types
.
public static void ThrowIfNotContainsInterface(Type argument, string paramName, string message, params Type[] types)
Parameters
argument
TypeThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
types
Type[]A variable number of System.Type arguments (that must be an interface) to match with the type of
argument
.
Exceptions
- System.ArgumentNullException
argument
is null - or -types
is null.- ArgumentOutOfRangeException
argument
is not contained within at least one of the specifiedtypes
.- ArgumentException
types
does not satisfy the condition of being an interface.
ThrowIfNotContainsInterface(Type, String, Type[])
Validates and throws an System.ArgumentOutOfRangeException if the specified argument
is not contained within at least one of the specified types
.
public static void ThrowIfNotContainsInterface(Type argument, string paramName, params Type[] types)
Parameters
argument
TypeThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
types
Type[]A variable number of System.Type arguments (that must be an interface) to match with the type of
argument
.
Exceptions
- System.ArgumentNullException
argument
is null - or -types
is null.- ArgumentOutOfRangeException
argument
is not contained within at least one of the specifiedtypes
.- ArgumentException
types
does not satisfy the condition of being an interface.
ThrowIfNotContainsInterface<T>(String, String, Type[])
Validates and throws a TypeArgumentOutOfRangeException if the specified T
is not contained within at least one of the specified types
.
public static void ThrowIfNotContainsInterface<T>(string typeParamName, string message, params Type[] types)
Parameters
typeParamName
StringThe name of the type parameter that caused the exception.
message
StringA message that describes the error.
types
Type[]A variable number of System.Type arguments (that must be an interface) to match with the type of
T
.
Type Parameters
T
Exceptions
- System.ArgumentNullException
types
is null.- TypeArgumentOutOfRangeException
T
is not contained within at least one of the specifiedtypes
.- ArgumentException
types
does not satisfy the condition of being an interface.
ThrowIfNotContainsInterface<T>(String, Type[])
Validates and throws a TypeArgumentOutOfRangeException if the specified T
is not contained within at least one of the specified types
.
public static void ThrowIfNotContainsInterface<T>(string typeParamName, params Type[] types)
Parameters
typeParamName
StringThe name of the type parameter that caused the exception.
types
Type[]A variable number of System.Type arguments (that must be an interface) to match with the type of
T
.
Type Parameters
T
Exceptions
- System.ArgumentNullException
types
is null.- TypeArgumentOutOfRangeException
T
is not contained within at least one of the specifiedtypes
.- ArgumentException
types
does not satisfy the condition of being an interface.
ThrowIfNotContainsType(Object, String, String, Type[])
Validates and throws an System.ArgumentOutOfRangeException if the specified argument
is not contained within at least one of the specified types
.
public static void ThrowIfNotContainsType(object argument, string paramName, string message, params Type[] types)
Parameters
argument
ObjectThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
types
Type[]A variable number of System.Type arguments to match with the type of
argument
.
Exceptions
- System.ArgumentNullException
argument
is null - or -types
is null.- ArgumentOutOfRangeException
argument
is not contained within at least one of the specifiedtypes
.
ThrowIfNotContainsType(Object, String, Type[])
Validates and throws an System.ArgumentOutOfRangeException if the specified argument
is not contained within at least one of the specified types
.
public static void ThrowIfNotContainsType(object argument, string paramName, params Type[] types)
Parameters
argument
ObjectThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
types
Type[]A variable number of System.Type arguments to match with the type of
argument
.
Exceptions
- System.ArgumentNullException
argument
is null - or -types
is null.- ArgumentOutOfRangeException
argument
is not contained within at least one of the specifiedtypes
.
ThrowIfNotContainsType(Type, String, String, Type[])
Validates and throws an System.ArgumentOutOfRangeException if the specified argument
is not contained within at least one of the specified types
.
public static void ThrowIfNotContainsType(Type argument, string paramName, string message, params Type[] types)
Parameters
argument
TypeThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
types
Type[]A variable number of System.Type arguments to match with the type of
argument
.
Exceptions
- System.ArgumentNullException
argument
is null - or -types
is null.- ArgumentOutOfRangeException
argument
is not contained within at least one of the specifiedtypes
.
ThrowIfNotContainsType(Type, String, Type[])
Validates and throws an System.ArgumentOutOfRangeException if the specified argument
is not contained within at least one of the specified types
.
public static void ThrowIfNotContainsType(Type argument, string paramName, params Type[] types)
Parameters
argument
TypeThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
types
Type[]A variable number of System.Type arguments to match with the type of
argument
.
Exceptions
- System.ArgumentNullException
argument
is null - or -types
is null.- ArgumentOutOfRangeException
argument
is not contained within at least one of the specifiedtypes
.
ThrowIfNotContainsType<T>(String, String, Type[])
Validates and throws a TypeArgumentOutOfRangeException if the specified T
is not contained within at least one of the specified types
.
public static void ThrowIfNotContainsType<T>(string typeParamName, string message, params Type[] types)
Parameters
typeParamName
StringThe name of the type parameter that caused the exception.
message
StringA message that describes the error.
types
Type[]A variable number of System.Type arguments to match with the type of
T
.
Type Parameters
T
Exceptions
- System.ArgumentNullException
types
is null.- TypeArgumentOutOfRangeException
T
is not contained within at least one of the specifiedtypes
.
ThrowIfNotContainsType<T>(String, Type[])
Validates and throws a TypeArgumentOutOfRangeException if the specified T
is not contained within at least one of the specified types
.
public static void ThrowIfNotContainsType<T>(string typeParamName, params Type[] types)
Parameters
typeParamName
StringThe name of the type parameter that caused the exception.
types
Type[]A variable number of System.Type arguments to match with the type of
T
.
Type Parameters
T
Exceptions
- System.ArgumentNullException
types
is null.- TypeArgumentOutOfRangeException
T
is not contained within at least one of the specifiedtypes
.
ThrowIfNotEmailAddress(String, String, String)
Validates and throws an System.ArgumentException if the specified argument
does not have the format of an email address.
public static void ThrowIfNotEmailAddress(string argument, string paramName = null, string message = "Value must be an email address.")
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Exceptions
- ArgumentException
argument
must be an email address.
ThrowIfNotEnum<TEnum>(String, String, Boolean, String)
Validates and throws an System.ArgumentException if the specified argument
does not represents an enumeration.
public static void ThrowIfNotEnum<TEnum>(string argument, string paramName = null, bool ignoreCase = true, string message = "Value does not represents an enumeration.")
where TEnum : struct, IConvertible
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
ignoreCase
Booleantrue
to ignore case;false
to regard case.message
StringA message that describes the error.
Type Parameters
TEnum
The type of the enumeration.
Exceptions
- ArgumentException
argument
does not represents an enumeration.
ThrowIfNotEnumType(Type, String, String)
Validates and throws an System.ArgumentException if the specified argument
does not represents an enumeration.
public static void ThrowIfNotEnumType(Type argument, string paramName = null, string message = "Value does not represents an enumeration.")
Parameters
argument
TypeThe type to check is not an enumeration.
paramName
StringThe name of the type parameter that caused the exception.
message
StringA message that describes the error.
Exceptions
- ArgumentException
argument
does not represents an enumeration.
ThrowIfNotEnumType<TEnum>(String, String)
Validates and throws a TypeArgumentException if the specified TEnum
does not represents an enumeration.
public static void ThrowIfNotEnumType<TEnum>(string typeParamName, string message = "Value does not represents an enumeration.")
Parameters
typeParamName
StringThe name of the type parameter that caused the exception.
message
StringA message that describes the error.
Type Parameters
TEnum
The type to check is not an enumeration.
Exceptions
- TypeArgumentException
TEnum
does not represents an enumeration.
ThrowIfNotEqual<T>(T, T, String, IEqualityComparer<T>, String)
Validates and throws an System.ArgumentOutOfRangeException if the specified x
object are not equal to the y
object.
public static void ThrowIfNotEqual<T>(T x, T y, string paramName, IEqualityComparer<T> comparer = null, string message = null)
Parameters
x
TThe first object to compare.
y
TThe second object to compare.
paramName
StringThe name of the parameter that caused the exception.
comparer
IEqualityComparer<T>The System.Collections.Generic.IEqualityComparer<T> implementation to use when comparing
x
andy
.message
StringA message that describes the error.
Type Parameters
T
The type of objects to compare.
Exceptions
- ArgumentOutOfRangeException
x
andy
are not equal to one another.
ThrowIfNotGuid(String, String, GuidFormats, String)
Validates and throws an System.ArgumentException if the specified argument
does not have the format of a System.Guid.
public static void ThrowIfNotGuid(string argument, string paramName = null, GuidFormats format = GuidFormats.D | GuidFormats.B | GuidFormats.P, string message = "Value must be a Guid.")
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
format
GuidFormatsA bitmask comprised of one or more GuidFormats that specify how the GUID parsing is conducted.
message
StringA message that describes the error.
Exceptions
- ArgumentException
argument
must be a System.Guid.
ThrowIfNotHex(String, String, String)
Validates and throws an System.ArgumentException if the specified argument
is not hexadecimal.
public static void ThrowIfNotHex(string argument, string paramName = null, string message = "Value must be hexadecimal.")
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Exceptions
- ArgumentException
argument
must be hexadecimal.
ThrowIfNotNumber(String, String, NumberStyles, IFormatProvider, String)
Validates and throws an System.ArgumentException if the specified argument
is not a number.
public static void ThrowIfNotNumber(string argument, string paramName = null, NumberStyles styles = NumberStyles.Number, IFormatProvider provider = null, string message = "Value must be a number.")
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
styles
NumberStylesA bitwise combination of System.Globalization.NumberStyles values that indicates the permitted format of
argument
.provider
IFormatProviderAn System.IFormatProvider that supplies culture-specific formatting information about
argument
.message
StringA message that describes the error.
Exceptions
- ArgumentException
argument
must be a number.
ThrowIfNotSame<T>(T, T, String, String)
Validates and throws an System.ArgumentOutOfRangeException if the specified x
object are not of the same instance as the y
object.
public static void ThrowIfNotSame<T>(T x, T y, string paramName, string message = null)
Parameters
x
TThe first object to compare.
y
TThe second object to compare.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Type Parameters
T
The type of objects to compare.
Exceptions
- ArgumentOutOfRangeException
x
andy
are not of the same instance.
ThrowIfNotUri(String, String, UriKind, String)
Validates and throws an System.ArgumentException if the specified argument
does not have the format of a System.Uri.
public static void ThrowIfNotUri(string argument, string paramName = null, UriKind uriKind = UriKind.Absolute, string message = "Value must be a URI.")
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
uriKind
UriKindThe type of the URI.
message
StringA message that describes the error.
Exceptions
- ArgumentException
argument
must be a System.Uri.- ArgumentOutOfRangeException
uriKind
was set to an indeterminate value of System.UriKind.RelativeOrAbsolute.
ThrowIfNull(Object, String, String)
Validates and throws an System.ArgumentNullException if the specified argument
is null.
public static void ThrowIfNull(object argument, string paramName = null, string message = "Value cannot be null.")
Parameters
argument
ObjectThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Exceptions
- System.ArgumentNullException
argument
cannot be null.
ThrowIfNull<T>(IDecorator<T>, out T, String, String)
Validates and throws an System.ArgumentNullException if the specified argument
is null.
public static void ThrowIfNull<T>(IDecorator<T> argument, out T inner, string paramName = null, string message = "Value cannot be null.")
Parameters
argument
IDecorator<T>The value to be evaluated.
inner
TThe inner object of
argument
.paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Type Parameters
T
The type of the inner object denoted by
argument
.
Exceptions
- System.ArgumentNullException
argument
cannot be null - or - IDecorator.Inner property ofargument
cannot be null.
ThrowIfNullOrEmpty(String, String)
Validates and throws either an System.ArgumentNullException or System.ArgumentException if the specified argument
is respectively null or empty.
public static void ThrowIfNullOrEmpty(string argument, string paramName = null)
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- System.ArgumentNullException
argument
cannot be null.- ArgumentException
argument
cannot be empty.
ThrowIfNullOrEmpty(String, String, String)
Validates and throws either an System.ArgumentNullException or System.ArgumentException if the specified argument
is respectively null or empty.
public static void ThrowIfNullOrEmpty(string argument, string paramName, string message)
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error to your liking.
Exceptions
- System.ArgumentNullException
argument
cannot be null.- ArgumentException
argument
cannot be empty.
ThrowIfNullOrWhitespace(String, String, String)
Validates and throws either an System.ArgumentNullException or System.ArgumentException if the specified argument
is respectively null, empty or consist only of white-space characters.
public static void ThrowIfNullOrWhitespace(string argument, string paramName = null, string message = null)
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Exceptions
- System.ArgumentNullException
argument
cannot be null.- ArgumentException
argument
cannot be empty or consist only of white-space characters.
ThrowIfNumber(String, String, NumberStyles, IFormatProvider, String)
Validates and throws an System.ArgumentException if the specified argument
is a number.
public static void ThrowIfNumber(string argument, string paramName = null, NumberStyles styles = NumberStyles.Number, IFormatProvider provider = null, string message = "Value cannot be a number.")
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
styles
NumberStylesA bitwise combination of System.Globalization.NumberStyles values that indicates the permitted format of
argument
.provider
IFormatProviderAn System.IFormatProvider that supplies culture-specific formatting information about
argument
.message
StringA message that describes the error.
Exceptions
- ArgumentException
argument
cannot be a number.
ThrowIfSame<T>(T, T, String, String)
Validates and throws an System.ArgumentOutOfRangeException if the specified x
object are of the same instance as the y
object.
public static void ThrowIfSame<T>(T x, T y, string paramName, string message = null)
Parameters
x
TThe first object to compare.
y
TThe second object to compare.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Type Parameters
T
The type of objects to compare.
Exceptions
- ArgumentOutOfRangeException
x
andy
are of the same instance.
ThrowIfSequenceEmpty<T>(IEnumerable<T>, String, String)
Validates and throws an System.ArgumentException if the specified argument
has no elements.
public static void ThrowIfSequenceEmpty<T>(IEnumerable<T> argument, string paramName, string message = "Value contains no elements.")
Parameters
argument
IEnumerable<T>The value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Type Parameters
T
Remarks
This method will not throw an exception if argument
is null.
Exceptions
- ArgumentException
argument
contains no elements.
ThrowIfSequenceNullOrEmpty<T>(IEnumerable<T>, String, String)
Validates and throws either an System.ArgumentNullException or System.ArgumentException if the specified argument
is respectively null or has no elements.
public static void ThrowIfSequenceNullOrEmpty<T>(IEnumerable<T> argument, string paramName, string message = "Value is either null or contains no elements.")
Parameters
argument
IEnumerable<T>The value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Type Parameters
T
Exceptions
- System.ArgumentNullException
argument
cannot be null.- ArgumentException
argument
contains no elements.
ThrowIfTrue(Boolean, String, String, String)
Validates and throws an System.ArgumentException if the specified condition
is true
.
public static void ThrowIfTrue(bool condition, string paramName, string message = "Value is not in a valid state.", string expression = null)
Parameters
condition
BooleanThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
expression
StringThe
condition
expressed as a string.
Exceptions
- ArgumentException
condition
istrue
.
ThrowIfTrue(Func<Boolean>, String, String, String)
Validates and throws an System.ArgumentException if the specified predicate
returns true
.
public static void ThrowIfTrue(Func<bool> predicate, string paramName, string message = "Value is not in a valid state.", string expression = null)
Parameters
predicate
Func<Boolean>The function delegate that determines if an System.ArgumentException is thrown.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
expression
StringThe
predicate
expressed as a string.
Exceptions
- ArgumentException
predicate
returnedtrue
.
ThrowIfUri(String, String, UriKind, String)
Validates and throws an System.ArgumentException if the specified argument
has the format of a System.Uri.
public static void ThrowIfUri(string argument, string paramName = null, UriKind uriKind = UriKind.Absolute, string message = "Value cannot be a URI.")
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
uriKind
UriKindThe type of the URI.
message
StringA message that describes the error.
Exceptions
- ArgumentException
argument
cannot be a System.Uri.- ArgumentOutOfRangeException
uriKind
was set to an indeterminate value of System.UriKind.RelativeOrAbsolute.
ThrowIfWhiteSpace(String, String, String)
Validates and throws an System.ArgumentException if the specified argument
consist only of white-space characters.
public static void ThrowIfWhiteSpace(string argument, string paramName, string message = "Value cannot consist only of white-space characters.")
Parameters
argument
StringThe value to be evaluated.
paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Remarks
This method will not throw an exception if argument
is null.
Exceptions
- ArgumentException
argument
cannot consist only of white-space characters.
ThrowWhen(Action<ExceptionCondition<ArgumentException>>)
Validates and throws an System.ArgumentException (or a derived counterpart) from the specified delegate condition
.
public static void ThrowWhen(Action<ExceptionCondition<ArgumentException>> condition)
Parameters
condition
Action<ExceptionCondition<ArgumentException>>The delegate that evaluates, creates and ultimately throws an System.ArgumentException (or a derived counterpart) from within a given scenario.
Exceptions
- System.ArgumentNullException
condition
is null.