I am trying to develop a mechanism by which I can execute any method with an attempt to retry.
A repeat will be triggered if an exception is encountered on the first run.
The basic idea: I will have a general class for repeat logic, and I want to pass any method in it through delegates. And this method will be executed with 1 repetition.
So far I have developed it.
public class RetryHandler
{
Delegate _methodToRetry;
public RetryHandler(Delegate MethodToExecuteWithRetry)
{
_methodToRetry = MethodToExecuteWithRetry;
}
public void ExecuteWithRetry(bool IsRetryEnabled)
{
try
{
_methodToRetry.DynamicInvoke();
}
catch (Exception ex)
{
if (IsRetryEnabled)
{
ExecuteWithRetry(false);
}
else
{
throw;
}
}
}
}
Now I have a problem:
The methods that I want to transfer have different input parameters (both in the number of parameters and in the types of objects) and different output parameters.
Is there any way I can achieve this? Basically I want to call such methods:
RetryHandler rh = new RetryHandler(MyMethod1(int a, int b));
int output = (int) rh.ExecuteWithRetry(true);
RetryHandler rh2 = new RetryHandler(MyMethod2(string a));
string output2 = (string) rh2.ExecuteWithRetry(true);
Any help would be greatly appreciated. Thank.
source