0

我有一些 C# 代码要添加到现有的 VB.net 项目中。C# 类被设计为一个 html 解析器

最初使用了在线转换器,并且能够让大部分课程正常工作,但下面的部分仍然无法正常工作。不幸的是,我缺乏解决此问题的知识。

我正在发布整篇文章,但如果有人可以澄清前几行,我认为这就足够了。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;
                }
            })},
    };
4

2 回答 2

0

这是哪个版本的vb.net?Lambda 表达式在每个表达式中的工作方式都略有不同。多行 lambda 仅在 VS2010+ 中受支持。对于 VS2008,您通常最终不得不将匿名方法转换为真实方法并通过AddressOf. 我注意到,从 C#->VB 转换匿名/lambda 类型表达式时,自动翻译通常似乎失败。

无论如何,与 C# 相比,VB 中的匿名方法语法有点笨拙和不同,以至于令人困惑。对于单行方法,你会做这样的事情(为了清楚起见,我使用简单的类型):

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))
于 2012-09-18T10:38:43.927 回答
0

您可以将此 C# 代码 1:1 转换为 VB.Net。

在这个例子中,我只添加了band div,但它应该足以指导你:

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)),因为它会大大提高可读性。

于 2012-09-18T11:20:36.573 回答