using System;
using System.Diagnostics;
using System.Linq;
namespace JD.Core
{
public static class CodeHelper
{
public class ForcedStopException : Exception
{
public ForcedStopException(string message)
{
Message = message;
}
public override string Message { get; }
public override string StackTrace { get { return base.StackTrace.Split('\n').Skip(1).Aggregate((first, next) => first + "\n" + next); } }
}
///
/// Breaks into the debugger if any is attached, otherwise throws a silent exception.
///
/// A message explaining why the executable stopped.
[DebuggerStepThrough]
public static void DebugBreak(string message)
{
if (Debugger.IsAttached)
{
Debugger.Break();
}
else
{
try
{
throw new ForcedStopException(message);
}
catch (Exception)
{
// ignored
}
}
}
///
/// Breaks into the debugger if any is attached, then throws an exception. This function does not return.
///
/// Always thrown.
/// A message explaining why the executable stopped.
[DebuggerStepThrough]
public static void ForceBreak(string message)
{
if (Debugger.IsAttached)
Debugger.Break();
throw new ForcedStopException(message);
}
public static T1 ExtractParameters(params object[] parameters)
{
if (parameters.Length < 1)
ForceBreak($"Not enough parameters. Required : 1, Found : {parameters.Length}");
if (!(parameters[0] is T1))
ForceBreak($"Type for parameter 0 does not match. Required : {typeof(T1).Name}, Found : {parameters[0].GetType().Name}");
return (T1)parameters[0];
}
public static Tuple ExtractParameters(params object[] parameters)
{
if (parameters.Length < 2)
ForceBreak($"Not enough parameters. Required : 2, Found : {parameters.Length}");
if (!(parameters[0] is T1))
ForceBreak($"Type for parameter 0 does not match. Required : {typeof(T1).Name}, Found : {parameters[0].GetType().Name}");
if (!(parameters[1] is T2))
ForceBreak($"Type for parameter 1 does not match. Required : {typeof(T2).Name}, Found : {parameters[1].GetType().Name}");
return new Tuple((T1)parameters[0], (T2)parameters[1]);
}
public static Tuple ExtractParameters(params object[] parameters)
{
if (parameters.Length < 3)
ForceBreak($"Not enough parameters. Required : 3, Found : {parameters.Length}");
if (!(parameters[0] is T1))
ForceBreak($"Type for parameter 0 does not match. Required : {typeof(T1).Name}, Found : {parameters[0].GetType().Name}");
if (!(parameters[1] is T2))
ForceBreak($"Type for parameter 1 does not match. Required : {typeof(T2).Name}, Found : {parameters[1].GetType().Name}");
if (!(parameters[2] is T3))
ForceBreak($"Type for parameter 2 does not match. Required : {typeof(T3).Name}, Found : {parameters[2].GetType().Name}");
return new Tuple((T1)parameters[0], (T2)parameters[1], (T3)parameters[2]);
}
public static Tuple ExtractParameters(params object[] parameters)
{
if (parameters.Length < 4)
ForceBreak($"Not enough parameters. Required : 4, Found : {parameters.Length}");
if (!(parameters[0] is T1))
ForceBreak($"Type for parameter 0 does not match. Required : {typeof(T1).Name}, Found : {parameters[0].GetType().Name}");
if (!(parameters[1] is T2))
ForceBreak($"Type for parameter 1 does not match. Required : {typeof(T2).Name}, Found : {parameters[1].GetType().Name}");
if (!(parameters[2] is T3))
ForceBreak($"Type for parameter 2 does not match. Required : {typeof(T3).Name}, Found : {parameters[2].GetType().Name}");
if (!(parameters[3] is T4))
ForceBreak($"Type for parameter 2 does not match. Required : {typeof(T4).Name}, Found : {parameters[3].GetType().Name}");
return new Tuple((T1)parameters[0], (T2)parameters[1], (T3)parameters[2], (T4)parameters[3]);
}
}
}