mirror of
https://github.com/farcasclaudiu/myfriendsaround.git
synced 2026-06-22 09:01:43 +03:00
00f97e41d6
http://windowsteamblog.com/windows_phone/b/wpdev/archive/2011/01/14/windows-push-notification-server-side-helper-library.aspx http://create.msdn.com/en-us/education/catalog/article/pnhelp-wp7
133 lines
5.5 KiB
C#
133 lines
5.5 KiB
C#
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Globalization;
|
|
|
|
using WindowsPhone.Recipes.Push.Messasges.Properties;
|
|
|
|
namespace WindowsPhone.Recipes.Push.Messasges
|
|
{
|
|
/// <summary>
|
|
/// A static helper class that includes various parameter checking routines.
|
|
/// </summary>
|
|
public static partial class Guard
|
|
{
|
|
/// <summary>
|
|
/// Throws <see cref="ArgumentNullException"/> if the given argument is null.
|
|
/// </summary>
|
|
/// <exception cref="ArgumentNullException"> if tested value if null.</exception>
|
|
/// <param name="argumentValue">Argument value to test.</param>
|
|
/// <param name="argumentName">Name of the argument being tested.</param>
|
|
public static void ArgumentNotNull(object argumentValue,
|
|
string argumentName)
|
|
{
|
|
if (argumentValue == null)
|
|
{
|
|
throw new ArgumentNullException(argumentName);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Throws an exception if the tested string argument is null or the empty string.
|
|
/// </summary>
|
|
/// <exception cref="ArgumentNullException">Thrown if string value is null.</exception>
|
|
/// <exception cref="ArgumentException">Thrown if the string is empty</exception>
|
|
/// <param name="argumentValue">Argument value to check.</param>
|
|
/// <param name="argumentName">Name of argument being checked.</param>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that an argument type is assignable from the provided type (meaning
|
|
/// interfaces are implemented, or classes exist in the base class hierarchy).
|
|
/// </summary>
|
|
/// <param name="assignmentTargetType">The argument type that will be assigned to.</param>
|
|
/// <param name="assignmentValueType">The type of the value being assigned.</param>
|
|
/// <param name="argumentName">Argument name.</param>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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).
|
|
/// </summary>
|
|
/// <param name="assignmentTargetType">The argument type that will be assigned to.</param>
|
|
/// <param name="assignmentInstance">The instance that will be assigned.</param>
|
|
/// <param name="argumentName">Argument name.</param>
|
|
[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;
|
|
}
|
|
}
|
|
}
|