gpt4 book ai didi

c# - C# 中异步委托(delegate)的 VB.NET 等价物是什么?

转载 作者:行者123 更新时间:2023-11-30 21:28:50 24 4
gpt4 key购买 nike

我正在尝试将以下扩展方法 ( source ) 从 C# 转换为 VB:

public static Task ForEachAsync<T>(this IEnumerable<T> source,
int dop, Func<T, Task> body)
{
return Task.WhenAll(
from partition in Partitioner.Create(source).GetPartitions(dop)
select Task.Run(async delegate {
using (partition)
while (partition.MoveNext())
await body(partition.Current);
}));
}

delegate 的常规等效项是Sub() , AFAIK,但我没想到它会在这种情况下工作,因为 Async关键字(它没有)。所以,我尝试使用 Function()相反:

<System.Runtime.CompilerServices.Extension>
Public Function ForEachAsync(Of T)(source As IEnumerable(Of T),
dop As Integer, body As Func(Of T, Task)) As Task
Return Task.WhenAll(
From partition In Partitioner.Create(source).GetPartitions(dop)
Select Task.Run(Async Function() 'As Task '<-- see below.
Using partition
Do While partition.MoveNext()
Await body(partition.Current)
Loop
End Using
End Function))
End Function

但这仍然无法编译并显示以下错误:

  • WhenAll :

    Overload resolution failed because no accessible 'WhenAll' can be called with these arguments:
    'Public Shared Overloads Function WhenAll(Of TResult)(tasks As IEnumerable(Of Task(Of TResult))) As Task(Of TResult())': Type parameter 'TResult' cannot be inferred.
    'Public Shared Overloads Function WhenAll(Of TResult)(ParamArray tasks As Task(Of TResult)()) As Task(Of TResult())': Type parameter 'TResult' cannot be inferred.
  • Await body(partition.Current) :

    'Await' may only be used in a query expression within the first collection expression of the initial 'From' clause or within the collection expression of a 'Join' clause.

  • [警告] Async Function() : (如果我添加 As Task 就会消失)

    Function '<anonymous method>' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.

我做错了什么?在 VB 中执行此操作的正确方法是什么?

最佳答案

在 C# 中,异步 Lambda 可以用 delegate type 表示或使用调用运算符 ()其次是 => token 作为 lambda 运算符调用匿名方法:

Task.Run(async ()=> { } );
Task.Run(async delegate { } );

在 VB.Net 中,可以使用 Lambda 表达式调用匿名方法 Sub()Function() , 内联和 Sub/End Sub , Function()/End Function block :

Task.Run(Async Sub() [operation on captured variables])
Task.Run(Sub()
[operation on captured variables]
End Sub))

Task.Run(Async Function() [operation on captured variables])
Task.Run(Function()
Return [operation on captured variables]
End Function))

VB.Net 的 LINQ to SQL不允许在 Select 中等待子句,因为:

Await may only be used in a query expression within the firstcollection expression of the initial From clause or within thecollection expression of a Join clause

它在 Stephen Toub 的 Async/Await FAQ 中被引用.

Select Task.Run(Async Function() ... )试图返回 IEnumerable(Of TResult)而不是 IEnumerable(Of Task) .

更多信息 Language-Integrated Query (LINQ) (Visual Basic) .

相反,LINQ to Objects - 使用 IEnumerable/IEnumerable<T>没有其他中间提供者的集合 - 允许在 Select 上使用异步/等待模式方法:

<Extension>
Public Function ForEachAsync(Of T)(source As IEnumerable(Of T), dop As Integer, body As Func(Of T, Task)) As Task
Return Task.WhenAll(
Partitioner.Create(source).GetPartitions(dop).
Select(Function(p) (
Task.Run(Async Function()
Using p
While p.MoveNext()
Await body(p.Current)
End While
End Using
End Function))))
End Function

LINQ to SQL 的 C# 版本允许它。
为什么,因为同样的规则也应该适用于 C# 实现?

The .NET Language Strategy :

C#:

We will keep growing C# to meet the evolving needs of developers andremain a state of the art programming language. We will innovateaggressively, while being very careful to stay within the spirit ofthe language.

VB.Net:

We will keep a focus on the cross-language tooling experience,recognizing that many VB developers also use C#. We will focusinnovation on the core scenarios and domains where VB is popular.

因此,VB and C# Coevolution断言在 2010 年发生了变化:C#VB.Net功能更新已分离。因此,鉴于新的语言策略,VB.NetC#不要再表现出大致相等的采用率

关于c# - C# 中异步委托(delegate)的 VB.NET 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55998804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com