This question has been asked in different ways before, but the answers don’t help me, because (1) I don’t have control over the built-in Exception classes, and (2) Activator.CreateInstance() returns an object/instance, where I need a true dynamic type.
I’m trying to create an extension method that allows me to throw a FaultException out of my WCF service, based off the exception I catch. For example:
try {
...
}
catch (ArgumentNullException exc) {
throw new FaultException<ArgumentNullException>(exc);
}
is straight-forward. But if I want to extend this in a general way, I’d use an extension class, like:
try {
...
}
catch (Exception exc) {
exc.ThrowFaultException();
}
Where I get hung up, of course, is the implementation of the extension method:
public static void ThrowFaultException(this Exception exc) {
// Gives me the correct type...
Type exceptionType = exc.GetType();
// But how the heck do I use it?
throw new FaultException<???>(exc);
}
Try this: