1

VB.NETでStringBuilderの拡張機能を作成して、AppendFormattedLineメソッドを追加しました(したがって、改行文字の引数の1つを使用する必要はありません)。

Imports System.Runtime.CompilerServices
Public Module sbExtension
    <Extension()> _
    Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                   ByVal format As String, _
                                   ByVal arg0 As Object)
        oStr.AppendFormat(format, arg0).Append(ControlChars.NewLine)
    End Sub
    <Extension()> _
    Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                   ByVal format As String, ByVal arg0 As Object, _
                                   ByVal arg1 As Object)
        oStr.AppendFormat(format, arg0, arg1).Append(ControlChars.NewLine)
    End Sub
    <Extension()> _
    Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                   ByVal format As String, _
                                   ByVal arg0 As Object, _
                                   ByVal arg1 As Object, _
                                   ByVal arg2 As Object)
        oStr.AppendFormat(format, arg0, arg1, arg2).Append(ControlChars.NewLine)
    End Sub
    <Extension()> _
   Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                  ByVal format As String, _
                                  ByVal ParamArray args() As Object)
        oStr.AppendFormat(format, args).Append(ControlChars.NewLine)
    End Sub
End Module
4

3 に答える 3

7

string.Format()私はそのような呼び出しをネストしません。

string.Format()それが舞台裏で新しいStringBuilderを作成し、それをAppendFormat()メソッドと呼ぶことをご存知ですか?例としてそこにある最初の方法を使用すると、これははるかに効率的であるはずです。

sb.AppendFormat(format, arg0).Append(Environment.NewLine);

VBコードにも同じ変更を加える必要があります。

于 2009-08-13T15:08:36.340 に答える
2

これが私が思いついた移植されたコードです:

using System;
using System.Text;

public static class ExtensionLibrary
{
    public static void AppendFormattedLine(this StringBuilder sb, string format, object arg0)
    {
        sb.AppendFormat(format, arg0).Append(Environment.NewLine);
    }
    public static void AppendFormattedLine(this StringBuilder sb, string format, object arg0, object arg1)
    {
        sb.AppendFormat(format, arg0, arg1).Append(Environment.NewLine);
    }
    public static void AppendFormattedLine(this StringBuilder sb, string format, object arg0, object arg1, object arg2)
    {
        sb.AppendFormat(format, arg0, arg1, arg2).Append(Environment.NewLine);
    }
    public static void AppendFormattedLine(this StringBuilder sb, string format, params object[] args)
    {
        sb.AppendFormat(format, args).Append(Environment.NewLine);
    }
}

うまくいけば、これは誰かに役立つでしょう!

改善されたコード、ジョエル、ルーク、ジェイソンに感謝します。

于 2009-08-13T15:03:05.490 に答える
0

私はこれまでTelerikのコードコンバーターを使用したことがありませんが、次のように考えています。

public class sbExtension
{
    [Extension()]
    public void AppendFormattedLine(System.Text.StringBuilder oStr, string format, object arg0)
    {
        oStr.AppendFormat(format, arg0).Append(ControlChars.NewLine);
    }
    [Extension()]
    public void AppendFormattedLine(System.Text.StringBuilder oStr, string format, object arg0, object arg1)
    {
        oStr.AppendFormat(format, arg0, arg1).Append(ControlChars.NewLine);
    }
    [Extension()]
    public void AppendFormattedLine(System.Text.StringBuilder oStr, string format, object arg0, object arg1, object arg2)
    {
        oStr.AppendFormat(format, arg0, arg1, arg2).Append(ControlChars.NewLine);
    }
    [Extension()]
    public void AppendFormattedLine(System.Text.StringBuilder oStr, string format, params object[] args)
    {
        oStr.AppendFormat(format, args).Append(ControlChars.NewLine);
    }
}


//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//Built and maintained by Todd Anglin and Telerik
//=======================================================
于 2009-08-13T15:24:27.193 に答える