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
-
Validator
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 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 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.
ThrowIfContainsAny(String, Char[], StringComparison, String, String)
Validates and throws an ArgumentOutOfRangeException if any of the characters
occurs within the argument
.
public static void ThrowIfContainsAny(string argument, char[] characters, StringComparison comparison = StringComparison.OrdinalIgnoreCase, string message = "One or more character matches were found.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
characters
Char[]The sequence of Char to search within
argument
.comparison
StringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
message
StringThe message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentOutOfRangeException
argument
contains one or more of the specifiedcharacters
.
ThrowIfContainsInterface(Type, Type[], String, String)
Validates and throws an ArgumentOutOfRangeException if the specified argument
is contained within at least one of the specified types
.
public static void ThrowIfContainsInterface(Type argument, Type[] types, string message = "Specified argument is contained within at least one of the specified types.", string paramName = null)
Parameters
argument
TypeThe value to be evaluated.
types
Type[]A Type array that contains zero or more types (that each must be an interface) to match with the type of
argument
.message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Remarks
Use ToArrayOf<T>(T[]) to substitute earlier signature of params Type[] types
.
Exceptions
- 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 Type arguments (that must be an interface) to match with the type of
T
.
Type Parameters
T
Exceptions
- 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 Type arguments (that must be an interface) to match with the type of
T
.
Type Parameters
T
Exceptions
- 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.
ThrowIfContainsReservedKeyword(String, IEnumerable<String>, IEqualityComparer<String>, String, String)
Validates and throws a ArgumentReservedKeywordException if the specified argument
is found in the sequence of reservedKeywords
.
public static void ThrowIfContainsReservedKeyword(string argument, IEnumerable<string> reservedKeywords, IEqualityComparer<string> comparer, string message = null, string paramName = null)
Parameters
argument
StringThe keyword to compare with
reservedKeywords
.reservedKeywords
IEnumerable<String>The reserved keywords to compare with
argument
.comparer
IEqualityComparer<String>The IEqualityComparer<T> implementation to use when comparing
reservedKeywords
withargument
.message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentReservedKeywordException
The specified
argument
is contained withinreservedKeywords
.
ThrowIfContainsReservedKeyword(String, IEnumerable<String>, String, String)
Validates and throws a ArgumentReservedKeywordException if the specified argument
is found in the sequence of reservedKeywords
.
public static void ThrowIfContainsReservedKeyword(string argument, IEnumerable<string> reservedKeywords, string message = null, string paramName = null)
Parameters
argument
StringThe keyword to compare with
reservedKeywords
.reservedKeywords
IEnumerable<String>The reserved keywords to compare with
argument
.message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentReservedKeywordException
The specified
argument
is contained withinreservedKeywords
.
ThrowIfContainsType(Object, Type[], String, String)
Validates and throws an ArgumentOutOfRangeException if the specified argument
is contained within at least one of the specified types
.
public static void ThrowIfContainsType(object argument, Type[] types, string message = "Specified argument is contained within at least one of the specified types.", string paramName = null)
Parameters
argument
ObjectThe value to be evaluated.
types
Type[]A Type array that contains zero or more types to match with the type of
argument
.message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Remarks
Use ToArrayOf<T>(T[]) to substitute earlier signature of params Type[] types
.
Exceptions
- ArgumentNullException
argument
is null - or -types
is null.- ArgumentOutOfRangeException
argument
is contained within at least one of the specifiedtypes
.
ThrowIfContainsType(Type, Type[], String, String)
Validates and throws an ArgumentOutOfRangeException if the specified argument
is contained within at least one of the specified types
.
public static void ThrowIfContainsType(Type argument, Type[] types, string message = "Specified argument is contained within at least one of the specified types.", string paramName = null)
Parameters
argument
TypeThe value to be evaluated.
types
Type[]A Type array that contains zero or more types to match with the type of
argument
.message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Remarks
Use ToArrayOf<T>(T[]) to substitute earlier signature of params Type[] types
.
Exceptions
- 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 Type arguments to match with the type of
T
.
Type Parameters
T
Exceptions
- 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 Type arguments to match with the type of
T
.
Type Parameters
T
Exceptions
- ArgumentNullException
types
is null.- TypeArgumentOutOfRangeException
T
is contained within at least one of the specifiedtypes
.
ThrowIfDifferent(String, String, String, String)
Validates and throws an ArgumentOutOfRangeException if there is a difference between second
and first
.
public static void ThrowIfDifferent(string first, string second, string paramName, string message = null)
Parameters
first
StringThe value that specifies valid characters.
second
StringThe value to compare with
first
.paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Exceptions
- ArgumentOutOfRangeException
There is a difference between
second
andfirst
.
ThrowIfDisposed(Boolean, Object, String)
Validates and throws an 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 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
- ObjectDisposedException
The
condition
istrue
.
ThrowIfDisposed(Boolean, Type, String)
Validates and throws an 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 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
- ObjectDisposedException
The
condition
istrue
.
ThrowIfEmailAddress(String, String, String)
Validates and throws an ArgumentException if the specified argument
has the format of an email address.
public static void ThrowIfEmailAddress(string argument, string message = "Value cannot be an email address.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentException
argument
cannot be an email address.
ThrowIfEmpty(String, String, String)
Validates and throws an ArgumentException if the specified argument
is empty.
public static void ThrowIfEmpty(string argument, string message = "Value cannot be empty.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Remarks
This method will not throw an exception if argument
is null.
Exceptions
- ArgumentException
argument
cannot be empty.
ThrowIfEnum<TEnum>(String, Boolean, String, String)
Validates and throws an ArgumentException if the specified argument
represents an enumeration.
public static void ThrowIfEnum<TEnum>(string argument, bool ignoreCase = true, string message = "Value represents an enumeration.", string paramName = null)
where TEnum : struct, IConvertible
Parameters
argument
StringThe value to be evaluated.
ignoreCase
Booleantrue
to ignore case;false
to regard case.message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Type Parameters
TEnum
The type of the enumeration.
Exceptions
- ArgumentException
argument
represents an enumeration.
ThrowIfEnumType(Type, String, String)
Validates and throws an ArgumentException if the specified argument
represents an enumeration.
public static void ThrowIfEnumType(Type argument, string message = "Value represents an enumeration.", string paramName = null)
Parameters
argument
TypeThe type to check is an enumeration.
message
StringA message that describes the error.
paramName
StringThe name of the type parameter that caused the exception.
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 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 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 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 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 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 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 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, GuidFormats, String, String)
Validates and throws an ArgumentException if the specified argument
has the format of a Guid.
public static void ThrowIfGuid(string argument, GuidFormats format = GuidFormats.D | GuidFormats.B | GuidFormats.P, string message = "Value cannot be a Guid.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
format
GuidFormatsA bitmask comprised of one or more GuidFormats that specify how the GUID parsing is conducted.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentException
argument
cannot be a Guid.
ThrowIfHex(String, String, String)
Validates and throws an ArgumentException if the specified argument
is hexadecimal.
public static void ThrowIfHex(string argument, string message = "Specified argument cannot be hexadecimal.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentException
argument
cannot be hexadecimal.
ThrowIfInvalidConfigurator<TOptions>(Action<TOptions>, out TOptions, String, String)
Validates and throws an ArgumentException if the specified argument
results in an instance of invalid options
.
public static void ThrowIfInvalidConfigurator<TOptions>(Action<TOptions> argument, out TOptions options, string message = "Delegate must configure the public read-write properties to be in a valid state.", string paramName = null)
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.message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
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 ArgumentException if the specified argument
are not in a valid state.
public static void ThrowIfInvalidOptions<TOptions>(TOptions argument, string message = "{0} are not in a valid state.", string paramName = null)
where TOptions : class, IParameterObject, new()
Parameters
argument
TOptionsThe configured options to validate.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
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
- ArgumentNullException
argument
cannot be null.- ArgumentException
argument
are not in a valid state.
ThrowIfInvalidState(Boolean, String, String)
Validates and throws an 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 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
- InvalidOperationException
condition
istrue
.
ThrowIfLowerThan<T>(T, T, String, String)
Validates and throws an 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 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 ArgumentOutOfRangeException if the specified argument
consist of anything besides a base-64 structure.
public static void ThrowIfNotBase64String(string argument, string message = "Value must consist only of base-64 digits.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentOutOfRangeException
argument
must consist only of base-64 digits.
ThrowIfNotBinaryDigits(String, String, String)
Validates and throws an ArgumentOutOfRangeException if the specified argument
consist of anything besides binary digits.
public static void ThrowIfNotBinaryDigits(string argument, string message = "Value must consist only of binary digits.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentOutOfRangeException
argument
must consist only of binary digits.
ThrowIfNotContainsAny(String, Char[], StringComparison, String, String)
Validates and throws an ArgumentOutOfRangeException if any of the characters
does not occur within the argument
.
public static void ThrowIfNotContainsAny(string argument, char[] characters, StringComparison comparison = StringComparison.OrdinalIgnoreCase, string message = "No matching characters were found.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
characters
Char[]The sequence of Char to search within
argument
.comparison
StringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
message
StringThe message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentOutOfRangeException
argument
does not contain any of the specifiedcharacters
.
ThrowIfNotContainsInterface(Type, Type[], String, String)
Validates and throws an ArgumentOutOfRangeException if the specified argument
is not contained within at least one of the specified types
.
public static void ThrowIfNotContainsInterface(Type argument, Type[] types, string message = "Specified argument is not contained within at least one of the specified types.", string paramName = null)
Parameters
argument
TypeThe value to be evaluated.
types
Type[]A Type array that contains zero or more types (that each must be an interface) to match with the type of
argument
.message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Remarks
Use ToArrayOf<T>(T[]) to substitute earlier signature of params Type[] types
.
Exceptions
- 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 Type arguments (that must be an interface) to match with the type of
T
.
Type Parameters
T
Exceptions
- 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 Type arguments (that must be an interface) to match with the type of
T
.
Type Parameters
T
Exceptions
- 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, Type[], String, String)
Validates and throws an ArgumentOutOfRangeException if the specified argument
is not contained within at least one of the specified types
.
public static void ThrowIfNotContainsType(object argument, Type[] types, string message = "Specified argument is not contained within at least one of the specified types.", string paramName = null)
Parameters
argument
ObjectThe value to be evaluated.
types
Type[]A Type array that contains zero or more types to match with the type of
argument
.message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Remarks
Use ToArrayOf<T>(T[]) to substitute earlier signature of params Type[] types
.
Exceptions
- ArgumentNullException
argument
is null - or -types
is null.- ArgumentOutOfRangeException
argument
is not contained within at least one of the specifiedtypes
.
ThrowIfNotContainsType(Type, Type[], String, String)
Validates and throws an ArgumentOutOfRangeException if the specified argument
is not contained within at least one of the specified types
.
public static void ThrowIfNotContainsType(Type argument, Type[] types, string message = "Specified argument is not contained within at least one of the specified types.", string paramName = null)
Parameters
argument
TypeThe value to be evaluated.
types
Type[]A Type array that contains zero or more types to match with the type of
argument
.message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Remarks
Use ToArrayOf<T>(T[]) to substitute earlier signature of params Type[] types
.
Exceptions
- 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 Type arguments to match with the type of
T
.
Type Parameters
T
Exceptions
- 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 Type arguments to match with the type of
T
.
Type Parameters
T
Exceptions
- ArgumentNullException
types
is null.- TypeArgumentOutOfRangeException
T
is not contained within at least one of the specifiedtypes
.
ThrowIfNotDifferent(String, String, String, String)
Validates and throws an ArgumentOutOfRangeException if there is no difference between second
and first
.
public static void ThrowIfNotDifferent(string first, string second, string paramName, string message = null)
Parameters
first
StringThe value that specifies valid characters.
second
StringThe value to compare with
first
.paramName
StringThe name of the parameter that caused the exception.
message
StringA message that describes the error.
Exceptions
- ArgumentOutOfRangeException
There is no difference between
second
andfirst
.
ThrowIfNotEmailAddress(String, String, String)
Validates and throws an ArgumentException if the specified argument
does not have the format of an email address.
public static void ThrowIfNotEmailAddress(string argument, string message = "Value must be an email address.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentException
argument
must be an email address.
ThrowIfNotEnum<TEnum>(String, Boolean, String, String)
Validates and throws an ArgumentException if the specified argument
does not represents an enumeration.
public static void ThrowIfNotEnum<TEnum>(string argument, bool ignoreCase = true, string message = "Value does not represents an enumeration.", string paramName = null)
where TEnum : struct, IConvertible
Parameters
argument
StringThe value to be evaluated.
ignoreCase
Booleantrue
to ignore case;false
to regard case.message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Type Parameters
TEnum
The type of the enumeration.
Exceptions
- ArgumentException
argument
does not represents an enumeration.
ThrowIfNotEnumType(Type, String, String)
Validates and throws an ArgumentException if the specified argument
does not represents an enumeration.
public static void ThrowIfNotEnumType(Type argument, string message = "Value does not represents an enumeration.", string paramName = null)
Parameters
argument
TypeThe type to check is not an enumeration.
message
StringA message that describes the error.
paramName
StringThe name of the type parameter that caused the exception.
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 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 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, GuidFormats, String, String)
Validates and throws an ArgumentException if the specified argument
does not have the format of a Guid.
public static void ThrowIfNotGuid(string argument, GuidFormats format = GuidFormats.D | GuidFormats.B | GuidFormats.P, string message = "Value must be a Guid.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
format
GuidFormatsA bitmask comprised of one or more GuidFormats that specify how the GUID parsing is conducted.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentException
argument
must be a Guid.
ThrowIfNotHex(String, String, String)
Validates and throws an ArgumentException if the specified argument
is not hexadecimal.
public static void ThrowIfNotHex(string argument, string message = "Value must be hexadecimal.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentException
argument
must be hexadecimal.
ThrowIfNotNumber(String, NumberStyles, IFormatProvider, String, String)
Validates and throws an ArgumentException if the specified argument
is not a number.
public static void ThrowIfNotNumber(string argument, NumberStyles styles = NumberStyles.Number, IFormatProvider provider = null, string message = "Value must be a number.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
styles
NumberStylesA bitwise combination of NumberStyles values that indicates the permitted format of
argument
.provider
IFormatProviderAn IFormatProvider that supplies culture-specific formatting information about
argument
.message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentException
argument
must be a number.
ThrowIfNotSame<T>(T, T, String, String)
Validates and throws an 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, UriKind, String, String)
Validates and throws an ArgumentException if the specified argument
does not have the format of a Uri.
public static void ThrowIfNotUri(string argument, UriKind uriKind = UriKind.Absolute, string message = "Value must be a URI.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
uriKind
UriKindThe type of the URI.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentException
argument
must be a Uri.- ArgumentOutOfRangeException
uriKind
was set to an indeterminate value of RelativeOrAbsolute.
ThrowIfNull(Object, String, String)
Validates and throws an ArgumentNullException if the specified argument
is null.
public static void ThrowIfNull(object argument, string message = "Value cannot be null.", string paramName = null)
Parameters
argument
ObjectThe value to be evaluated.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentNullException
argument
cannot be null.
ThrowIfNull<T>(IDecorator<T>, out T, String, String)
Validates and throws an ArgumentNullException if the specified argument
is null.
public static void ThrowIfNull<T>(IDecorator<T> argument, out T inner, string message = "Value cannot be null.", string paramName = null)
Parameters
argument
IDecorator<T>The value to be evaluated.
inner
TThe inner object of
argument
.message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Type Parameters
T
The type of the inner object denoted by
argument
.
Exceptions
- ArgumentNullException
argument
cannot be null - or - IDecorator.Inner property ofargument
cannot be null.
ThrowIfNullOrEmpty(String, String, String)
Validates and throws either an ArgumentNullException or ArgumentException if the specified argument
is respectively null or empty.
public static void ThrowIfNullOrEmpty(string argument, string message = null, string paramName = null)
Parameters
argument
StringThe value to be evaluated.
message
StringA message that describes the error to your liking.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentNullException
argument
cannot be null.- ArgumentException
argument
cannot be empty.
ThrowIfNullOrWhitespace(String, String, String)
Validates and throws either an ArgumentNullException or ArgumentException if the specified argument
is respectively null, empty or consist only of white-space characters.
public static void ThrowIfNullOrWhitespace(string argument, string message = null, string paramName = null)
Parameters
argument
StringThe value to be evaluated.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentNullException
argument
cannot be null.- ArgumentException
argument
cannot be empty or consist only of white-space characters.
ThrowIfNumber(String, NumberStyles, IFormatProvider, String, String)
Validates and throws an ArgumentException if the specified argument
is a number.
public static void ThrowIfNumber(string argument, NumberStyles styles = NumberStyles.Number, IFormatProvider provider = null, string message = "Value cannot be a number.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
styles
NumberStylesA bitwise combination of NumberStyles values that indicates the permitted format of
argument
.provider
IFormatProviderAn IFormatProvider that supplies culture-specific formatting information about
argument
.message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentException
argument
cannot be a number.
ThrowIfSame<T>(T, T, String, String)
Validates and throws an 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 ArgumentException if the specified argument
has no elements.
public static void ThrowIfSequenceEmpty<T>(IEnumerable<T> argument, string message = "Value contains no elements.", string paramName = null)
Parameters
argument
IEnumerable<T>The value to be evaluated.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
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 ArgumentNullException or ArgumentException if the specified argument
is respectively null or has no elements.
public static void ThrowIfSequenceNullOrEmpty<T>(IEnumerable<T> argument, string message = "Value is either null or contains no elements.", string paramName = null)
Parameters
argument
IEnumerable<T>The value to be evaluated.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Type Parameters
T
Exceptions
- ArgumentNullException
argument
cannot be null.- ArgumentException
argument
contains no elements.
ThrowIfTrue(Boolean, String, String, String)
Validates and throws an 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 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 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, UriKind, String, String)
Validates and throws an ArgumentException if the specified argument
has the format of a Uri.
public static void ThrowIfUri(string argument, UriKind uriKind = UriKind.Absolute, string message = "Value cannot be a URI.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
uriKind
UriKindThe type of the URI.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
Exceptions
- ArgumentException
argument
cannot be a Uri.- ArgumentOutOfRangeException
uriKind
was set to an indeterminate value of RelativeOrAbsolute.
ThrowIfWhiteSpace(String, String, String)
Validates and throws an ArgumentException if the specified argument
consist only of white-space characters.
public static void ThrowIfWhiteSpace(string argument, string message = "Value cannot consist only of white-space characters.", string paramName = null)
Parameters
argument
StringThe value to be evaluated.
message
StringA message that describes the error.
paramName
StringThe name of the parameter that caused the exception.
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 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 ArgumentException (or a derived counterpart) from within a given scenario.
Exceptions
- ArgumentNullException
condition
is null.