将C#转换为VB.NET:在对象初始值设定项中初始化的字段或属性的名称必须以'开头。'

时间:2015-07-01 14:57:28

标签: c# asp.net vb.net

我在github找到了解决我问题的方法 https://github.com/brandon-barker/PushBots.NET
但是在C#中,我试图在很多站点中转换它,但它们都会抛出错误 这里是原始的C#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PushBots.NET;
using PushBots.NET.Models;
using PushBots.NET.Enums;
using System.Threading.Tasks;

public partial class Default3 : System.Web.UI.Page
{
        protected void Button1_Click(object sender, EventArgs e)
        {
            Run().Wait();
        }
        static async Task Run()
        {
            var client = new PushBotsClient("55165801f85f8b457d", "b03052506824b4f3165ecc0");
            var pushMessage = new BatchPush()
            {
                Message = "new Test from API",
                Badge = "+1",
                Platforms = new[] { Platform.Android, Platform.iOS }
            };
            var result = await client.Push(pushMessage);
        }
}

它工作正常但是当我将静态异步任务Run()转换为VB时,我在VB中得到了很多错误

Private Shared Function Run() As Task
    Dim client = New PushBotsClient("55165801f85f8b457d", "b03052506824b4f3165ecc0")
Dim pushMessage = New BatchPush() With { _
    Key .Message = "new Test from API", _
    Key .Badge = "+1", _
    Key .Platforms = New () {Platform.Android, Platform.iOS} _
}
    Dim result = Await client.Push(pushMessage)
End Function

我得到的错误是Key (在对象初始值设定项中初始化的字段或属性的名称必须以'开头。'。)

第二个错误是Await (Await只能在Async方法中使用)

2 个答案:

答案 0 :(得分:3)

有几件事需要解决:

  • 在方法声明中添加Async

    Private Shared Async Function Run() As Task
    
  • Key块中删除With。在C# - > VB转换期间添加Keycorrect for anonymous classes,但对于非匿名类属性初始化则不行。

    换句话说,
    C#new { ID = 3 }对应于VB New With { Key .ID = 3 },但是 C#new MyClass { ID = 3 }应该成为New MyClass With { .ID = 3 }

  • 隐式类型数组不需要
  • New(),即.Platforms = {Platform.Android, Platform.iOS}就足够了。

答案 1 :(得分:1)

您需要在功能签名中添加Async

Private Shared Async Function Run() As Task

https://msdn.microsoft.com/en-us/library/hh191564.aspx