Skip to content

Instantly share code, notes, and snippets.

@restlessmedia
Created September 24, 2015 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save restlessmedia/7d353c5eb519dd065071 to your computer and use it in GitHub Desktop.
Save restlessmedia/7d353c5eb519dd065071 to your computer and use it in GitHub Desktop.
private static Exception Create<T>(string message, T exception)
where T : Exception
{
Type exceptionType = typeof(T);
if (typeof(Exception).IsAssignableFrom(exceptionType))
return new Exception(message, exception);
MethodInfo method = _createDel.Method.GetGenericMethodDefinition().MakeGenericMethod(exceptionType);
return method.Invoke(null, new object[] { message, exception }) as Exception;
}
private static Exception Create<T>(string message, Exception innerException)
where T : Exception
{
Type type = typeof(T);
Type[] argTypes = new[] { typeof(string), typeof(Exception) };
ConstructorInfo ctor = type.GetConstructor(argTypes);
if (ctor == null)
return null;
try
{
return (T)ctor.Invoke(new object[] { message, innerException });
}
catch
{
return null;
}
}
private static Delegate _createDel = (Func<string, Exception, Exception>)ExceptionFilter.Create<Exception>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment