Is there a way to extend the using
block in C# in such a way that takes a delegate as a second parameter alongside an IDisposable object and executes every time when an exception is thrown inside that using
block?
Imagine we have a delegate, something like this:
public delegate void ExceptionHandler(Exception ex);
And suppose I have a method that matches that delegate, something like this:
public void Log(Exception ex)
{
// Some logging stuff goes here
}
And I want to accomplish something like this:
using(SqlConnection connection = new SqlConnection(""), Log)
{
}
Is there a way to extend C# in such a way?