using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using WindowsPhone.Recipes.Push.Messasges.Properties;
namespace WindowsPhone.Recipes.Push.Messasges
{
///
/// A static helper class that includes various parameter checking routines.
///
public static partial class Guard
{
///
/// Throws if the given argument is null.
///
/// if tested value if null.
/// Argument value to test.
/// Name of the argument being tested.
public static void ArgumentNotNull(object argumentValue,
string argumentName)
{
if (argumentValue == null)
{
throw new ArgumentNullException(argumentName);
}
}
///
/// Throws an exception if the tested string argument is null or the empty string.
///
/// Thrown if string value is null.
/// Thrown if the string is empty
/// Argument value to check.
/// Name of argument being checked.
public static void ArgumentNotNullOrEmpty(string argumentValue,
string argumentName)
{
if (argumentValue == null)
{
throw new ArgumentNullException(argumentName);
}
if (argumentValue.Length == 0)
{
throw new ArgumentException(Resources.ArgumentMustNotBeEmpty, argumentName);
}
}
///
/// Verifies that an argument type is assignable from the provided type (meaning
/// interfaces are implemented, or classes exist in the base class hierarchy).
///
/// The argument type that will be assigned to.
/// The type of the value being assigned.
/// Argument name.
public static void TypeIsAssignable(Type assignmentTargetType, Type assignmentValueType, string argumentName)
{
if (assignmentTargetType == null)
{
throw new ArgumentNullException("assignmentTargetType");
}
if (assignmentValueType == null)
{
throw new ArgumentNullException("assignmentValueType");
}
if (!assignmentTargetType.IsAssignableFrom(assignmentValueType))
{
throw new ArgumentException(string.Format(
CultureInfo.CurrentCulture,
Resources.TypesAreNotAssignable,
assignmentTargetType,
assignmentValueType),
argumentName);
}
}
///
/// Verifies that an argument instance is assignable from the provided type (meaning
/// interfaces are implemented, or classes exist in the base class hierarchy, or instance can be
/// assigned through a runtime wrapper, as is the case for COM Objects).
///
/// The argument type that will be assigned to.
/// The instance that will be assigned.
/// Argument name.
[SuppressMessage(
"Microsoft.Design",
"CA1031:DoNotCatchGeneralExceptionTypes",
Justification = "GetType() invoked for diagnostics purposes")]
public static void InstanceIsAssignable(Type assignmentTargetType, object assignmentInstance, string argumentName)
{
if (assignmentTargetType == null)
{
throw new ArgumentNullException("assignmentTargetType");
}
if (assignmentInstance == null)
{
throw new ArgumentNullException("assignmentInstance");
}
if (!assignmentTargetType.IsInstanceOfType(assignmentInstance))
{
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
Resources.TypesAreNotAssignable,
assignmentTargetType,
GetTypeName(assignmentInstance)),
argumentName);
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes",
Justification = "Need to use exception as flow control here, no other choice")]
private static string GetTypeName(object assignmentInstance)
{
string assignmentInstanceType;
try
{
assignmentInstanceType = assignmentInstance.GetType().FullName;
}
catch (Exception)
{
assignmentInstanceType = Resources.UnknownType;
}
return assignmentInstanceType;
}
}
}