107 lines
4.8 KiB
C#
107 lines
4.8 KiB
C#
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); } }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Breaks into the debugger if any is attached, otherwise throws a silent exception.
|
|
/// </summary>
|
|
/// <param name="message">A message explaining why the executable stopped. </param>
|
|
[DebuggerStepThrough]
|
|
public static void DebugBreak(string message)
|
|
{
|
|
if (Debugger.IsAttached)
|
|
{
|
|
Debugger.Break();
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
throw new ForcedStopException(message);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Breaks into the debugger if any is attached, then throws an exception. This function does not return.
|
|
/// </summary>
|
|
/// <exception cref="System.InvalidOperationException">Always thrown. </exception>
|
|
/// <param name="message">A message explaining why the executable stopped. </param>
|
|
[DebuggerStepThrough]
|
|
public static void ForceBreak(string message)
|
|
{
|
|
if (Debugger.IsAttached)
|
|
Debugger.Break();
|
|
|
|
throw new ForcedStopException(message);
|
|
}
|
|
|
|
public static T1 ExtractParameters<T1>(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<T1, T2> ExtractParameters<T1, T2>(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, T2>((T1)parameters[0], (T2)parameters[1]);
|
|
}
|
|
|
|
public static Tuple<T1, T2, T3> ExtractParameters<T1, T2, T3>(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, T2, T3>((T1)parameters[0], (T2)parameters[1], (T3)parameters[2]);
|
|
}
|
|
|
|
public static Tuple<T1, T2, T3, T4> ExtractParameters<T1, T2, T3, T4>(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, T2, T3, T4>((T1)parameters[0], (T2)parameters[1], (T3)parameters[2], (T4)parameters[3]);
|
|
}
|
|
}
|
|
}
|