在下面,我需要通过nextDB
的Lambda表达式中重试:C#传递参数与λ
Retry.Do(() =>
{
string nextDB = dbList.Next();
using (DataBaseProxy repo = new DataBaseProxy(nextDB))
{
return repo.DoSomething();
}
});
我该怎么办呢?这里是我Retry
类:
public static class Retry
{
public static void Do(
Action action,
int retryCount = 3)
{
Do<object>(() =>
{
action();
return null;
}, retryCount);
}
public static T Do<T>(
Func<T> action,
int retryCount = 3)
{
var exceptions = new List<Exception>();
for (int retry = 0; retry < retryCount; retry++)
{
try
{
return action();
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
throw new AggregateException(exceptions);
}
}
@DavidG - 我相信是这样 - 我还是希望它运行在待办事项块中的代码 - 但我希望能够通过一个字符串动作 –
@DavidG不,我不需要返回任何东西。是的 - 我只有一个参数。干杯 –