DebugHelper Component
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
namespace Sample.Diagnostics
{
///
/// Utility class that helps diagnose problems by retrieving information about the current debug context.
///
/// This includes information such as the currently loaded assemblies.
public sealed class DebugHelper
{
#region Construction
///
/// Restricted Constructor
///
private DebugHelper()
{
}
#endregion
#region Static Methods
///
/// Asserts and the throws, making sure that the error gets handled by client code
///
/// The type of the exception to throw.
/// the asserted condition
/// The failure message.
[SuppressMessage("Microsoft.Design", "CA1004", Justification = "This method instantiates and throws the exception, it can't be passed in")]
[DebuggerStepThrough()]
public static void AssertAndThrow(bool condition, string failureMessage) where TException : Exception
{
Debug.Assert(condition, failureMessage);
if (condition == false)
{
ConstructorInfo ctor = typeof(TException).GetConstructor(new Type[] { typeof(string) });
TException exception = (TException)ctor.Invoke(new object[] { failureMessage });
throw exception;
}
}
///
/// Asserts and the throws, making sure that the error gets handled by client code
///
/// The type of the exception to throw.
/// the asserted condition
[SuppressMessage("Microsoft.Design", "CA1004", Justification = "This method instantiates and throws the exception, it can't be passed in")]
[DebuggerStepThrough()]
public static void AssertAndThrow(bool condition) where TException : Exception
{
Debug.Assert(condition, "Assertion Failed");
if (condition == false)
{
ConstructorInfo ctor = typeof(TException).GetConstructor(null);
TException exception = (TException)ctor.Invoke(null);
throw exception;
}
}
#endregion
}
}
No comments:
Post a Comment