[英]How do I port an extension in VB.NET to C#?
我在 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
我不會像那樣嵌套string.Format()
調用。
您知道string.Format()
在幕后創建一個新的 StringBuilder 並調用它的AppendFormat()
方法嗎? 以第一種方法為例,這應該效率更高:
sb.AppendFormat(format, arg0).Append(Environment.NewLine);
您也應該對 VB 代碼進行相同的更改。
這是我想出的移植代碼:
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。
我以前從未使用過 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
//=======================================================
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.