Yesterday I wanted to create an ExceptionHandler
class to which for example we could pass:
- the exception
- the method where that happened
- the parameters given to that method
The ExceptionHandler
would implement a custom business logic and re-execute automatically (or not) the method with the same parameters.
The closest solution I found was by using a delegate
like this
private void button1_Click(object sender, EventArgs e)
{
InvokeMyMethod(Test, "string", 1);
InvokeMyMethod(TestN, "other string", 3, Color.Red);
}
public delegate void MyMethodDelegate(params object[] args);
public void InvokeMyMethod(MyMethodDelegate method, params object[] args)
{
method.DynamicInvoke(args);
}
public void Test(params object[] args)
{
if (args.Length < 2) return;
MessageBox.Show(string.Format("{0}{1}", args[0], args[1]));
}
public void TestN(params object[] args)
{
// or, whatewer
if (args.Length < 3) return;
MessageBox.Show(string.Format("{0}{1}{2}", args[0], args[1], args[2]));
}
However this solution is still not perfect because of the methods' signature. I don't want all my methods receive a unique parameter params object[] args
, moreover this is often not possible directly for example with all UI related events (Winform, ASP.NET, WPF or whatever).
So I'd like to know if there is a way to pass one by one arguments to a delegate and then execute the function only when we are ready?
For example here is an imaginary code with non-existing methods AddArgument
and Execute
public void InvokeMyMethod(MyMethodDelegate method, params object[] args)
{
foreach(var arg in args)
{
method.AddArgument(arg);
}
method.Execute();
}
By this way all our functions could keep their original signature and receive multiple arguments instead of an array.