Created
September 24, 2015 09:55
-
-
Save restlessmedia/7d353c5eb519dd065071 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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