如何将VB.NET中的扩展名移植到C#? [英] How do I port an extension in VB.NET to C#?
本文介绍了如何将VB.NET中的扩展名移植到C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在VB.NET中为StringBuilder编写了一个扩展,以添加 AppendFormattedLine
方法(这样,我就不必在换行符中使用其中一个参数):
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
推荐答案
以下是我想到的已移植代码:
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);
}
}
希望这对某人有用!
改进的代码,谢谢joel,luke&杰森.
这篇关于如何将VB.NET中的扩展名移植到C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文