代码之家  ›  专栏  ›  技术社区  ›  Anders

如何将vb.net中的扩展移植到c_?

  •  1
  • Anders  · 技术社区  · 16 年前

    我在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
    
    3 回复  |  直到 16 年前
        1
  •  7
  •   Joel Coehoorn    16 年前

    我不会筑巢 string.Format() 这样的电话。

    你知道吗? string.format()。 在后台创建一个新的StringBuilder并调用它 AppendFormat() 方法?以上面的第一种方法为例,这应该更有效:

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

    你也应该对你的VB代码做同样的修改。

        2
  •  2
  •   Anders    16 年前

    以下是我提出的移植代码:

    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。

        3
  •  0
  •   Chris    16 年前

    我以前从未使用过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
    //=======================================================