I wrote this extension for stringbuilder in VB.NET:
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("{0}{1}", String.Format(format, arg0), 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("{0}{1}", String.Format(format, arg0, arg1), 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("{0}{1}", String.Format(format, arg0, arg1, arg2), 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("{0}{1}", String.Format(format, args), ControlChars.NewLine)
End Sub
End Module
I tried porting it to C# (I am slowly learning/teaching myself C#), but I have been unsuccessful thus far:
using System.Runtime.CompilerServices;
namespace myExtensions
{
public static class sbExtension
{
[Extension()]
public static void AppendFormattedLine(this System.Text.StringBuilder oStr, string format, string arg0)
{
oStr.AppendFormat("{0}{1}", string.Format(format, arg0), Environment.NewLine);
}
[Extension()]
public static void AppendFormattedLine(this System.Text.StringBuilder oStr, string format, string arg0, string arg1)
{
oStr.AppendFormat("{0}{1}", string.Format(format, arg0, arg1), Environment.NewLine);
}
[Extension()]
public static void AppendFormattedLine(this System.Text.StringBuilder oStr, string format, string arg0, string arg1, string arg2)
{
oStr.AppendFormat("{0}{1}", string.Format(format, arg0, arg1, arg2), Environment.NewLine);
}
[Extension()]
public static void AppendFormattedLine(this System.Text.StringBuilder oStr, string format, string[] args)
{
oStr.AppendFormat("{0}{1}", string.Format(format, args), Environment.NewLine);
}
}
}
When I have this file in my App_Code folder, I get the following error message:
Compiler Error Message: CS1112: Do not use 'System.Runtime.CompilerServices.ExtensionAttribute'. Use the 'this' keyword instead.
I am unsure of what it means by this, because if I replace '[Extension()]' with 'this', the only thing that is related to an extension is 'ExtensionAttribute', but that does not work either.
Does anyone know what I am missing?
Thanks!
C#'s "this" keyword, which you are using correctly, is a replacement for the [Extension()] attribute. Remove those and you should be good to go.
To clarify, by "replacement" I really mean "syntactic sugar" - when the compiler sees the "this" modifier, it generates the same ExtensionAttribute you have to add by hand in VB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With