views:

194

answers:

3

Hey SO,

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!

+13  A: 

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.

dahlbyk
ahh thank you for an explanation! cheers
Anders
+6  A: 

Just remove the Extension attribute altogether -- C# only requires the this qualifier on the first parameter to create an extension method.

Ben M
A: 

You'll want to change the namespace to System, along with removing the Extension attribute as others have said.

Bela
Why would they want to change the namespace to System?
Steven Sudit