2012-09-18 45 views
0

我有一些C#代碼添加到現有的VB.net項目。 C#類被設計爲一個html解析器C#VBNET轉換問題

已經初步使用在線轉換器,並且能夠獲得大部分類的工作,但下面的部分仍然無法正常工作。不幸的是我缺乏解決這個問題的知識。

我張貼整篇文章,但如果有人可以澄清第一對夫婦,我認爲這就夠了。 AttributeNameValuePair是保存屬性的單獨的類。

進一步下來一些內聯函數的使用,將欣賞這樣的例子。或者將它們作爲單獨的函數並僅在內部留下引用會更容易?

非常感謝您的幫助。

  private readonly Dictionary<string, Action<DocumentModel, IEnumerable<AttributeNameValuePair>>> commandsDictionary = new Dictionary<string, Action<DocumentModel, IEnumerable<AttributeNameValuePair>>>() 
    { 
     { "b", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Bold = true) }, 
     { "i", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Italic = true) }, 
     { "u", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.UnderlineStyle = UnderlineType.Single) }, 
     { "strike", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Strikethrough = true) }, 
     { "sub", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Subscript = true) }, 
     { "sup", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Superscript = true) }, 
     { "div", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => 
      { 
       foreach(var arg in args) 
       { 
        if(arg.AttributeName == "align") 
        { 
         HorizontalAlignment align; 
         switch(arg.AttributeValue) 
         { 
          case "center": 
           align = HorizontalAlignment.Center; 
           break; 
          case "right": 
           align = HorizontalAlignment.Right; 
           break; 
          case "justify": 
           align = HorizontalAlignment.Justify; 
           break; 
          default: 
           align = HorizontalAlignment.Left; 
           break; 
         } 
        } 
       } 
      })}, 
     { "br", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => doc.Sections[0].Blocks.Add(new Paragraph(doc))) }, 
     { "span", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => {})}, 
     { "font", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => 
      { 
       foreach(AttributeNameValuePair arg in args) 
       { 
        int? size = null; 
        string fontName = null; 

        // Some dummy values. 
        if (arg.AttributeName == "size") 
         size = 10 + 3 * int.Parse(arg.AttributeValue); 
        else if (arg.AttributeName == "face") 
         fontName = arg.AttributeValue.Split(',').First(); 

        var lastFormat = GetLastRun(doc).CharacterFormat; 
        if (size.HasValue) 
         lastFormat.Size = size.Value; 

        if (fontName != null) 
         lastFormat.FontName = fontName; 
       } 
      })}, 
    }; 

回答

0

哪個版本的vb.net是這樣的? Lambda表達式在每個表達式中都有點不同。多行lambda僅在VS2010 +中受支持。對於VS2008,通常最終不得不將匿名方法變成真正的方法,並通過AddressOf來引用它們。自動翻譯,我注意到,通常在從C# - > VB轉換匿名/ lambda類型表達式時失敗。

在任何情況下,與C#相比,VB中的匿名方法語法有點笨拙,並且與C#相比有些不同,令人困惑。對於單行法,你會做這樣的事情(我使用的是簡單類型爲清楚起見):

Private cmdDictionary As New Dictionary(Of String, Action(Of Integer, String)) 

'adding items' 
cmdDictionary.Add("div", New Action(Of Integer, String) _ 
     (Sub(x As Integer, y As String) Console.WriteLine(y & ":" & CStr(x)))) 

'then to access the dictionary' 
cmdDictionary.Item("div")(42, "foobar") 

對於多(在VS2010)的模式如下:

cmdDictionary.Add("div", New Action(Of Integer, String) _ 
         (Sub(x As Integer, y As String) 
          x = 2 * x 
          Console.WriteLine(y & ":" & CStr(x)) 
         End Sub)) 
+0

對不起,我正在使用VS2010,感謝您的信息。 – pophatis

+0

@pophatis增加了一些例子,然後 –

+0

謝謝J我能夠跨過這個障礙,並進一步。能夠解決一些其他問題,只是很高興得到一些編碼完成和進步。因此,沒有得到周圍感謝你遠行,謝謝! – pophatis

0

可以轉換這個C#代碼1:1到VB.Net。

在這個例子中,我只加bdiv,但它應該足以指導您:

Private Readonly commandsDictionary As Dictionary(Of string, Action(Of DocumentModel, IEnumerable(Of AttributeNameValuePair))) = 
new Dictionary(Of string, Action(Of DocumentModel, IEnumerable(Of AttributeNameValuePair)))() From 
{ 
    { "b", new Action(Of DocumentModel, IEnumerable(Of AttributeNameValuePair)) (Sub(doc, args) GetLastRun(doc).CharacterFormat.Bold = true) }, 
    { "div", new Action(Of DocumentModel, IEnumerable(Of AttributeNameValuePair))(Sub(doc, args) 
      For Each arg in args 
       if arg.AttributeName = "align" Then 
        Dim align as HorizontalAlignment 
        Select arg.AttributeValue 
         case "center": 
          align = HorizontalAlignment.Center 
         case "right": 
          align = HorizontalAlignment.Right 
         case "justify": 
          align = HorizontalAlignment.Justify 
         case else: 
          align = HorizontalAlignment.Left 
        end select 
       end if 
      Next 
    End Sub)}} 
} 

你也許想創建一個類型別名Action(Of DocumentModel, IEnumerable(Of AttributeNameValuePair)),因爲這將極大地提高可讀性。

+0

謝謝牛排先生嘗試了幾個符號,但最終以您的示例結束,因爲這非常合適。感謝您花時間。感覺我需要在VB中學習我的編程技巧,但也可能對C#有一個基本的把握。在網上尋找信息當我嘗試將自己的解決方案組合起來時,我傾向於迷失方向,尤其是當大多數都是用C#編寫的時候。 – pophatis