我正在尝试在 ASP.NET 4.5 Web 窗体中执行一些异步操作。我在遵循几个在线教程时收到此错误:
Overload resolution failed because no accessible 'New' can be called with these arguments:
'Public Sub New(handler As System.Func(Of System.Threading.CancellationToken, System.Threading.Tasks.Task))': Value of type 'System.Threading.Tasks.Task' cannot be converted to 'System.Func(Of System.Threading.CancellationToken, System.Threading.Tasks.Task)'.
'Public Sub New(handler As System.Func(Of System.Threading.Tasks.Task))': Value of type 'System.Threading.Tasks.Task' cannot be converted to 'System.Func(Of System.Threading.Tasks.Task)'.
这是代码(类似于 this tutorial ):
Imports System.Threading.Tasks
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
RegisterAsyncTask(New PageAsyncTask(DoStuffAsync))
' etc...
End Sub
Private Async Function DoStuffAsync() As Task
Await SomeTask()
End Function
这是解决方案中唯一的错误。据我所知,这正是教程中的内容(从 C# 翻译而来)。我确实在 MSDN 文档中注意到 PageAsyncTask
对事件处理程序使用了几个附加参数(这需要附加代码),但我在线阅读的所有教程都避开了这些参数,只输入了函数名称(Func(任务)
)。我错过了什么?
This是一个示例,其中该人仅使用方法名称(并传递方法的参数)。
最佳答案
在 C# 中,您可以仅使用方法名称来传递函数委托(delegate)作为参数。在 VB.NET 中,您需要使用 AddressOf
运算符来传递委托(delegate)。例如
RegisterAsyncTask(New PageAsyncTask(AddressOf DoStuffAsync))
关于asp.net - New PageAsyncTask 的重载解析失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27573832/