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

And*_*ers 5 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
Run Code Online (Sandbox Code Playgroud)

我尝试将它移植到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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

有谁知道我错过了什么?

谢谢!

dah*_*byk 15

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

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


Eri*_*der 11

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

public static void ApplyNewServer(this ReportDocument report, string serverName, string username, string password)
{
}
Run Code Online (Sandbox Code Playgroud)


Ben*_*n M 10

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