namespace ScriptCs
{
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
///
/// Provides guard clauses.
///
internal static class Guard
{
///
/// Guards against a null argument.
///
/// The type of the argument.
/// Name of the parameter.
/// The argument.
/// is null.
/// is restricted to reference types to avoid boxing of value type objects.
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")]
[DebuggerStepThrough]
public static void AgainstNullArgument(string parameterName, [ValidatedNotNull]TArgument argument) where TArgument : class
{
if (argument == null)
{
throw new ArgumentNullException(parameterName, string.Format(CultureInfo.InvariantCulture, "{0} is null.", parameterName));
}
}
///
/// Guards against a null argument if can be null.
///
/// The type of the argument.
/// Name of the parameter.
/// The argument.
/// is null.
///
/// Performs a type check to avoid boxing of value type objects.
///
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")]
[DebuggerStepThrough]
public static void AgainstNullArgumentIfNullable(string parameterName, [ValidatedNotNull]TArgument argument)
{
if (typeof(TArgument).IsNullableType() && argument == null)
{
throw new ArgumentNullException(parameterName, string.Format(CultureInfo.InvariantCulture, "{0} is null.", parameterName));
}
}
///
/// Guards against a null argument property value.
///
/// The type of the property.
/// Name of the parameter.
/// Name of the property.
/// The argument property.
/// is null.
/// is restricted to reference types to avoid boxing of value type objects.
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")]
[DebuggerStepThrough]
public static void AgainstNullArgumentProperty(string parameterName, string propertyName, [ValidatedNotNull]TProperty argumentProperty)
where TProperty : class
{
if (argumentProperty == null)
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "{0}.{1} is null.", parameterName, propertyName), parameterName);
}
}
///
/// Guards against a null argument property value if can be null.
///
/// The type of the property.
/// Name of the parameter.
/// Name of the property.
/// The argument property.
/// is null.
///
/// Performs a type check to avoid boxing of value type objects.
///
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")]
[DebuggerStepThrough]
public static void AgainstNullArgumentPropertyIfNullable(
string parameterName, string propertyName, [ValidatedNotNull]TProperty argumentProperty)
{
if (typeof(TProperty).IsNullableType() && argumentProperty == null)
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "{0}.{1} is null.", parameterName, propertyName), parameterName);
}
}
///
/// Determines whether the specified type is a nullable type.
///
/// The type.
///
/// true if the specified type is a nullable type; otherwise, false.
///
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")]
private static bool IsNullableType(this Type type)
{
return !type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>));
}
///
/// When applied to a parameter, this attribute provides an indication to code analysis that the argument has been null checked.
///
private sealed class ValidatedNotNullAttribute : Attribute
{
}
}
}