c# - Fixing 'method group' issue for delegate -


inspired cleanest way write retry logic? made this

    public static t retry<t>(int numretries, int timeout, delegate method, params object[] args)     {         if (method == null)             throw new argumentnullexception("method");          var retval = default(t);                 {             try             {                 retval = (t) method.dynamicinvoke(args);                 return retval;             }             catch (timeoutexception)             {                 if (numretries <= 0)                     throw; // avoid silent failure                 thread.sleep(timeout);             }         } while (numretries-- > 0);          return retval;     } 

however i've run method group problem.

test setup

    private int add(int x, int y)     {         return x + y;     }      public static void main(string[] args)     {         var r = retry<int>(5, 10, add, 1, 1);     } 

is there no better way fix other retry<int>(5, 10, new func<int, int, int>(add), 1, 1);

you can change retry to

public static t retry<t>(int numretries, int timeout, func<t> method) {     if (method == null)         throw new argumentnullexception("method");      var retval = default(t);         {         try         {             retval = method();             return retval;         }         catch (timeoutexception)         {             if (numretries <= 0)                 throw; // avoid silent failure             thread.sleep(timeout);         }     } while (numretries-- > 0);      return retval; } 

and call as

var result = retry(5, 10, ()=>add(1,1)); 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -