using System;
using System.Collections.Generic;
using System.Reflection;
namespace Chernobyl.Reflection
{
///
/// Thrown when an error involving method(s) occurs.
///
public class MethodException : Exception
{
///
/// Initializes a new instance of the class
/// whose is set to null.
///
/// The error message that explains the reason for
/// the exception.
/// The methods that are involved in the error.
public MethodException(string message, IEnumerable methods)
: this(message, methods, null)
{}
///
/// Initializes a new instance of the class.
///
/// The error message that explains the reason for
/// the exception.
/// The methods that are involved in the error.
/// The exception that is the cause of the
/// current exception, or a null reference (Nothing in Visual Basic) if
/// no inner exception is specified.
public MethodException(string message, IEnumerable methods, Exception innerException)
: base(message, innerException)
{
Methods = methods;
}
///
/// The methods that are involved in the error.
///
public IEnumerable Methods { get; private set; }
}
///
/// Extension and utility methods for and
/// related types.
///
public static class MethodExceptionExtensions
{
///
/// Throws an when the provided
/// instance's
/// equals .
///
/// The method whose
/// is to be checked.
/// True to throw exception when
/// is true, false to throw exception
/// when is false.
/// Thrown when the provided
/// instance's
/// equals .
public static void ThrowWhenStaticIs(this MethodBase method, bool value)
{
if (method.IsStatic == value)
{
string message = string.Format(
value ? "The method '{0}' must not be static." :
"The method '{0}' must be static.", method);
throw new MethodException(message, method.ToEnumerable());
}
}
}
}