NullReference
NullReference

Reputation: 4484

How to use C# Action with an object?

I'm trying to create a generic retry policy that passes in a object and a function to call on that object. I know it's possible with a method but I'm not sure how to include the object.

For example I have an api object that has many different methods that I'd like to pass as the action. If a certain type of exception occurs I'd need to update values in the api object.

Any ideas? thanks

 public T Do<T>(Api api, Func<T> action, TimeSpan retryInterval, int retryCount = 3)
 {
        var exceptions = new List<Exception>();

        for (int retry = 0; retry < retryCount; retry++)
        {
             try
             {
                    return api.action();
             }
             catch (Exception ex)
             {

Upvotes: 1

Views: 75

Answers (1)

leppie
leppie

Reputation: 117250

You want Func<Api, T> and call it as action(api)

Upvotes: 3

Related Questions