Extension method to Exception requiring dynamic generic type?

Try this: public static void ThrowFaultException(this TException ex) where TException : System. Exception { throw new FaultException(ex); }.

Works perfectly! Exactly what I wanted : ) – James B Dec 3 '10 at 19:25 I constantly forget the beauty of generics! – James B Dec 3 '10 at 19:26.

You don't need to cast the object returned by Activator. CreateInstance to FaultException to throw it. Casting it to Exception is enough: var type = typeof(FaultException).

MakeGenericType(exc.GetType()); throw (Exception)Activator. CreateInstance(type, exc); I wouldn't throw the exception in ThrowFaultException though: try { ... } catch (Exception e) { throw e. WrapInFaultException(); } public static Exception WrapInFaultException(this Exception e) { var type = typeof(FaultException).

MakeGenericType(e.GetType()); return (Exception)Activator. CreateInstance(type, e); }.

– Pandincus Dec 3 '10 at 19:02 @Pandincus: I haven't tested it, but I believe the call to Activator. CreateInstance(Type, Object) shown above should work as-is. – dtb Dec 3 '10 at 19:04 Nevermind, I see you edited your answer for that.

Neat trick! – Pandincus Dec 3 '10 at 19:05 If I do that the client catches a general exception, not a FaultException : ( – James B Dec 3 '10 at 19:23.

Public static void ThrowFaultException(this Exception exc) { // Gives me the correct type... Type exceptionType = exc.GetType(); var genericType = typeof(FaultException). MakeGenericType(exceptionType); // But how the heck do I use it? Throw (Exception)Activator.

CreateInstance(genericType, exc); }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions