2015-02-05 46 views
1

我试图执行错误处理我的计划之内的组件类,但不断收到以下错误消息:的方法类型参数不能从辅助型的使用推断

错误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; 
      } 
     } 
    } 

回答

1

您的lambda的内容是SetPlotList(Convert.ToInt32(FilterTextBox.Text))

SetPlotList返回void,但Invoke假定提供给它的lambda返回一些(非void)类型。它不能推断出lambda返回的类型,因为它不返回类型SetPlotList返回了一些东西,那么Invoke调用将正常工作。

+0

如何调整'Invoke'调用来迎合返回void和non-void类型的方法? – methuselah 2015-02-05 16:48:53

+0

@methuselah它需要接受一个不返回值的委托。如果它也不需要接受任何参数,那么可以使用“Action”。当然,如果考虑到委托人没有回报价值的事实,该方法的实施将需要显着改变。如果你想能够同时拥有,你可以编写第二个非泛型重载。 – Servy 2015-02-05 16:50:15

0

SetPlotList是空隙的方法,但ErrorHandlingComponent.Invoke期望一个Func<T>作为它的第一个参数。它需要调用Func并返回func的返回值。由于您试图将它传递给void方法,编译器会抱怨。

+0

如何调整'Invoke'调用以迎合返回void和non-void类型的方法? – methuselah 2015-02-05 16:49:38

相关问题