我试图执行错误处理我的计划之内的组件类,但不断收到以下错误消息:的方法类型参数不能从辅助型的使用推断
错误1个类型参数的方法 'Marketplace.ErrorHandlingComponent.Invoke(System.Func,int, System.TimeSpan)'不能从使用情况中推断出来。尝试明确指定类型参数 。
我不知道什么是尝试指定类型参数明确意思。
MainWindow.xaml.cs
public void SetPlotList(int filterReference)
{
// Fill plot list view
List<PlotComponent.PlotList> plotList = PlotComponent.SelectPlotLists(filterReference);
// Find the plot list item in the new list
PlotComponent.PlotList selectPlotList =
plotList.Find(x => Convert.ToInt32(x.PlotId) == _focusPlotReference);
Dispatcher.Invoke(
(() =>
{
PlotListView.ItemsSource = plotList;
if (selectPlotList != null)
{
PlotListView.SelectedItem = selectPlotList;
}
}));
int jobSum = 0;
int bidSum = 0;
foreach (PlotComponent.PlotList item in PlotListView.Items)
{
jobSum += Convert.ToInt32(item.Jobs);
bidSum += Convert.ToInt32(item.Bids);
}
// Determine job/bid list ratio
Dispatcher.BeginInvoke(
new ThreadStart(() => JobBidRatioTextBlock.Text = jobSum + " jobs - " + bidSum + " bids"));
}
private void ValidateTextbox()
{
if (Regex.IsMatch(FilterTextBox.Text, "[^0-9]") || FilterTextBox.Text == "") return;
try
{
ErrorHandlingComponent.Invoke(() => SetPlotList(Convert.ToInt32(FilterTextBox.Text)), 3, TimeSpan.FromSeconds(1));
}
catch
{
FilterTextBox.Text = null;
}
}
ErrorHandlingComponent.cs
public static T Invoke<T>(Func<T> func, int tryCount, TimeSpan tryInterval)
{
if (tryCount < 1)
{
throw new ArgumentOutOfRangeException("tryCount");
}
while (true)
{
try
{
return func();
}
catch (Exception ex)
{
if (--tryCount > 0)
{
Thread.Sleep(tryInterval);
continue;
}
LogError(ex.ToString());
throw;
}
}
}
如何调整'Invoke'调用来迎合返回void和non-void类型的方法? – methuselah 2015-02-05 16:48:53
@methuselah它需要接受一个不返回值的委托。如果它也不需要接受任何参数,那么可以使用“Action”。当然,如果考虑到委托人没有回报价值的事实,该方法的实施将需要显着改变。如果你想能够同时拥有,你可以编写第二个非泛型重载。 – Servy 2015-02-05 16:50:15