How do I port an extension in VB.NET to C#?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 09:21:00

问题


I wrote an extension in VB.NET for StringBuilder to add a AppendFormattedLine method (so I would not have to use one of the arguments for a new line character):

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

回答1:


I wouldn't nest the string.Format() calls like that.

Did you know that string.Format() creates a new StringBuilder behind the scenes and calls it's AppendFormat() method? Using the first method up there as an example, this should be much more efficient:

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

You should make the same change to your VB code as well.




回答2:


Here is the ported code that I came up with:

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

Hopefully this will come in useful for someone!

improved code, thanks joel, luke & jason.




回答3:


I've never used Telerik's code converter before, but here is what it thinks:

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
//=======================================================


来源:https://stackoverflow.com/questions/1272534/how-do-i-port-an-extension-in-vb-net-to-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!