无法从辅助类型中的用法推断出方法的类型参数

Type arguments for method cannot be inferred from the usage in helper type

我正在尝试在我的程序中执行错误处理组件 class,但不断收到以下错误消息:

Error 1 The type arguments for method 'Marketplace.ErrorHandlingComponent.Invoke(System.Func, int, System.TimeSpan)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

我不确定尝试明确指定类型参数是什么意思

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

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

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

SetPlotList returns void,但是 Invoke 假定提供给它的 lambda returns 某种(非空)类型。它无法推断出 lambda returns 的类型,因为它 return 不是一个类型 SetPlotList return编辑了一些东西,然后 Invoke 调用将正常运行。