General method for executing any method with 1 retry using delegates

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;

    // default constructor with delegate to function name.
    public RetryHandler(Delegate MethodToExecuteWithRetry)
    {
        _methodToRetry = MethodToExecuteWithRetry;
    }

    public void ExecuteWithRetry(bool IsRetryEnabled)
    {
        try
        {
            _methodToRetry.DynamicInvoke();
        }
        catch (Exception ex)
        {
            if (IsRetryEnabled)
            {
                // re execute method.
                ExecuteWithRetry(false);
            }
            else
            {
                // throw exception
                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.

+4
source
2
  • :

       public void ExecuteWithRetry(bool IsRetryEnabled, params object[] args)
        {
            try
            {
                _methodToRetry.DynamicInvoke(args);
            }
            catch (Exception ex)
            {
                if (IsRetryEnabled)
                {
                    // re execute method.
                    ExecuteWithRetry(false, args);
    

    : int output = (int) rh.ExecuteWithRetry(true, a, b);

  • rwrite RetryHandler , :

    public static TReturn ExecuteWithRetry<TReturn, TParam>(bool IsRetryEnabled, Func<TParam, TReturn> func, TParam param)
    {
        // ...
        return func(param);
        // ...
    }
    
  • , :

    var f = new Func<int, int>((a) => a + 1);
    f.ExecuteWithRetry(x);
    

    :

    public static class Ext
    {
        public static TReturn ExecuteWithRetry<TReturn, TParam>(this Func<TParam, TReturn> func, TParam param)
        {
            return func(param);
        }
    }
    
+2

:

public T Retry<T>(Func<T> func)
{
    try { return func(); }
    catch { return func(); }
}

-, :

public int Test(int a, int b) => a + b;
public string Test(string a) => a + a;

void Example()
{
    Console.WriteLine(Retry(() => Test(1, 2)));
    Console.WriteLine(Retry(() => Test("a")));
}
+3

Source: https://habr.com/ru/post/1693367/


All Articles