在下面,我需要通過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不,我不需要返回任何東西。是的 - 我只有一個參數。乾杯 –