Extension method to Exception requiring dynamic generic type

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);
}

The answers here and here don't help. Any ideas?

Answers


Try this:

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

Need Your Help

WCF Ria Services entities and INotifyPropertyChanged

silverlight wcf-ria-services inotifypropertychanged

I am having an issue where the PropertyChanged event from the generated Ria Services entities is not raised for all the properties.

Newbie question for Flex programming

flex actionscript-3 actionscript

I made a custom itemRenderer, and need to access a variable in my Project file (mxml). How can I make my public var available in the custom itemRenderer file?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.