C# 无法从助手类型中的用法推断方法的类型参数
我试图在程序中执行错误处理组件类,但不断收到以下错误消息: 方法的类型参数出现错误1 'Marketplace.ErrorHandlingComponent.Invoke(System.Func,int, 无法从用法推断System.TimeSpan“”。尝试指定 类型参数被显式删除 我不确定是什么意思,请尝试显式指定类型参数 MainWindow.xaml.csC# 无法从助手类型中的用法推断方法的类型参数,c#,wpf,C#,Wpf,我试图在程序中执行错误处理组件类,但不断收到以下错误消息: 方法的类型参数出现错误1 'Marketplace.ErrorHandlingComponent.Invoke(System.Func,int, 无法从用法推断System.TimeSpan“”。尝试指定 类型参数被显式删除 我不确定是什么意思,请尝试显式指定类型参数 MainWindow.xaml.cs public void SetPlotList(int filterReference) { //
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;
}
}
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;
}
}
}
public void SetPlotList(int filterReference)
{
//填充打印列表视图
List plotList=PlotComponent。选择plotList(filterReference);
//在新列表中查找打印列表项
PlotComponent.PlotList选择PlotList=
plotList.Find(x=>Convert.ToInt32(x.PlotId)==\u focusPlotReference);
调度程序。调用(
(() =>
{
PlotListView.ItemsSource=plotList;
如果(selectPlotList!=null)
{
PlotListView.SelectedItem=selectPlotList;
}
}));
int jobSum=0;
int bidSum=0;
foreach(PlotListView.Items中的PlotComponent.PlotList项)
{
jobSum+=转换为32(项作业);
bidSum+=转换为32(项目投标);
}
//确定工作/投标清单比率
调度程序。开始启动(
新的ThreadStart(()=>jobbiratiotextblock.Text=jobSum+“jobs-”+bidSum+“bids”);
}
私有void ValidateTextbox()
{
if(Regex.IsMatch(FilterTextBox.Text,[^0-9]”)返回;
尝试
{
ErrorHandlingComponent.Invoke(()=>SetPlotList(Convert.ToInt32(FilterTextBox.Text)),3,TimeSpan.FromSeconds(1));
}
抓住
{
FilterTextBox.Text=null;
}
}
错误处理组件.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;
}
}
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;
}
}
}
publicstatict调用(Func-Func、int-tryCount、TimeSpan-tryInterval)
{
如果(tryCount<1)
{
抛出新ArgumentOutOfRangeException(“tryCount”);
}
while(true)
{
尝试
{
返回func();
}
捕获(例外情况除外)
{
如果(--tryCount>0)
{
线程。睡眠(tryInterval);
继续;
}
日志错误(例如ToString());
投掷;
}
}
}
SetPlotList
是一个无效方法,但ErrorHandlingComponent.Invoke
需要一个Func
作为其第一个参数。它需要调用Func并返回Func的返回值。由于您试图传递一个void方法,编译器会抱怨。lambda的内容是SetPlotList(Convert.ToInt32(FilterTextBox.Text))
SetPlotList
返回void
,但是Invoke
假设提供给它的lambda返回一些(非void)类型。它无法推断lambda返回的类型,因为它不返回类型SetPlotList
返回了一些内容,那么Invoke
调用将正常运行。我如何调整Invoke
调用以适应返回无效和非无效类型的方法?@methuselah需要接受不返回值的委托。如果它还需要不接受任何参数,则操作
将适用。当然,如果要考虑委托没有返回值这一事实,则需要对方法的实现进行重大修改。如果希望能够同时使用这两种类型,可以编写第二个非泛型重载。如何调整Invoke
调用以适应返回void和non-void类型的方法?