你如何在C#中创建扩展?

时间:2009-07-24 15:28:54

标签: c# extension-methods

我在VB.NET中为stringbuilder编写了这个扩展:

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

我尝试将其移植到C#(我正在慢慢地学习/自学C#),但到目前为止我没有成功:

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);
        }
    }
}

当我在App_Code文件夹中有此文件时,收到以下错误消息:

  

编译器错误消息: CS1112:不要使用'System.Runtime.CompilerServices.ExtensionAttribute'。请改用“this”关键字。

我不确定这意味着什么,因为如果我用'this'替换'[Extension()]',那么唯一与扩展相关的是'ExtensionAttribute',但这也不起作用。

有谁知道我错过了什么?

谢谢!

5 个答案:

答案 0 :(得分:15)

您正确使用的C#的“this”关键字是[Extension()]属性的替代品。删除那些,你应该很高兴。

澄清一下,通过“替换”我的意思是“语法糖” - 当编译器看到“this”修饰符时,它会生成你必须在VB中手动添加的相同ExtensionAttribute。

答案 1 :(得分:11)

实际上没有人显示语法示例。

public static void ApplyNewServer(this ReportDocument report, string serverName, string username, string password)
{
}

答案 2 :(得分:10)

完全删除Extension属性 - C#只需要第一个参数上的this限定符来创建扩展方法。

答案 3 :(得分:0)

你使用的属性是扩展方法,在C#中你不是手动编写的,编译器会为你做,... 你可以在这里学习,如何使用VB和VB在.Net中编写扩展方法C#

http://www.codeproject.com/Articles/261639/Extension-Methods-in-NET

http://www.codeproject.com/Articles/34295/Extend-the-NET-Library-with-Extension-Methods

答案 4 :(得分:-1)

您需要将命名空间更改为System,同时删除Extension属性,正如其他人所说的那样。