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

时间:2019-02-14 01:56:49

标签: c# vb.net lambda hangfire

我很难将C#语句转换为VB。我不知道这是不是我确实使用过的lambda,但我不是那个专家,也不是分隔符,对此我了解得更少。根据Microsoft的说法,C#'=>' symbol可以以两种不同的方式使用。

我遇到麻烦的C#代码行是:

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

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

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")

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

1 个答案:

答案 0 :(得分:2)

VB.NET具有two different lambda expressionsFunctionSub,后者不返回值。

在这种情况下,您要使用Sub,因为表达式不返回值。

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