如何将此 C# lambda 转换为 VB.net?

How do I translate this C# lambda into VB.net?

我在将 C# 语句翻译成 VB 时遇到困难。我不知道这是否是一个 lambda,我确实使用过但我不是专家,或者是一个分隔符,我对它的了解更少。根据 Microsoft 的说法,C# '=>' symbol 可以以这两种不同的方式使用。

我遇到问题的 C# 代码行是:

RecurringJob.AddOrUpdate(() => TextBuffer.WriteLine("text"), Cron.Minutely);

它来自 sample hangfire.mvcApplication project 中的 Startup.cs 文件。这是完整的 Class:

public void Configuration(IAppBuilder app)
{
    GlobalConfiguration.Configuration.UseSqlServerStorage("DefaultConnection");
    app.UseHangfireDashboard();
    app.UseHangfireServer();
    RecurringJob.AddOrUpdate(
        () => TextBuffer.WriteLine("Recurring Job completed successfully!"), 
        Cron.Minutely);
}

the TextBuffer.WriteLine reference is:

public static void WriteLine(string value)
{
    lock (Buffer)
    {
        Buffer.AppendLine(String.Format("{0} {1}", DateTime.Now, value));
    }
}

这是我试过的,不太准确;我尝试了很多变化:

RecurringJob.AddOrUpdate(Function() TextBuffer.WriteLine("text"), Cron.Minutely)

我在 Visual Studio 中遇到的语法错误是:

Expression does not produce a value

我查看了 AddOrUpdate 的定义并看到了以下重载,但我不确定使用哪个是正确的选择,也不确定选择正确重载后语法的外观。

Public Shared Sub AddOrUpdate(methodCall As Expression(Of Action), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(methodCall As Expression(Of Action), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(methodCall As Expression(Of Func(Of Task)), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(methodCall As Expression(Of Func(Of Task)), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(recurringJobId As String, methodCall As Expression(Of Action), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(recurringJobId As String, methodCall As Expression(Of Action), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(recurringJobId As String, methodCall As Expression(Of Func(Of Task)), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(recurringJobId As String, methodCall As Expression(Of Func(Of Task)), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(methodCall As Expression(Of Action(Of T)), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(methodCall As Expression(Of Action(Of T)), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(methodCall As Expression(Of Func(Of T, Task)), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(methodCall As Expression(Of Func(Of T, Task)), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(recurringJobId As String, methodCall As Expression(Of Action(Of T)), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(recurringJobId As String, methodCall As Expression(Of Action(Of T)), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(recurringJobId As String, methodCall As Expression(Of Func(Of T, Task)), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(recurringJobId As String, methodCall As Expression(Of Func(Of T, Task)), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")

我只是不明白该方法在寻找什么或如何正确编写它。

VB.NET 有 two different lambda expressions: Function,其中 return 是一个值,而 Sub 不是。

在这种情况下,您想使用 Sub,因为您的表达式没有 return 值。

RecurringJob.AddOrUpdate(Sub() TextBuffer.WriteLine("text"), Cron.Minutely)