1

我在 VB.NET 中为 StringBuilder 编写了一个扩展来添加一个AppendFormattedLine方法(所以我不必为换行符使用其中一个参数):

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);
    }
}

希望这对某人有用!

改进的代码,感谢 joel、luke 和 jason。

于 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 回答