0

既存のVB.netプロジェクトに追加するC#コードがいくつかあります。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 ですか? ラムダ式は、それぞれで少し異なる動作をします。複数行のラムダは、VS2010+ でのみサポートされています。VS2008 の場合、通常、匿名メソッドを実際のメソッドに変換し、 経由で参照する必要がありますAddressOf。自動変換は、C#->VB から匿名/ラムダ型の式を変換するときに、一般的に失敗するようです。

いずれにせよ、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))
于 2012-09-18T10:38:43.927 に答える
0

この C# コードを 1:1 で VB.Net に変換できます。

この例では、 と のみを追加bしまし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 に答える