如何在 C# 中创建扩展?
我在 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”,但这也不起作用。
有谁知道我缺少什么?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要将命名空间更改为 System,并像其他人所说的那样删除 Extension 属性。
You'll want to change the namespace to System, along with removing the Extension attribute as others have said.
您使用的属性用于扩展方法,在 C# 中您无需手动编写它,编译器会为您完成它,...
您可以在这里学习如何使用 VB 和 .Net 编写扩展方法。 C#
http://www.codeproject.com/Articles/261639/Extension- NET 中的方法
http: //www.codeproject.com/Articles/34295/Extend-the-NET-Library-with-Extension-Methods
the attribute you use is for extension method, which in C# you do not write it manually and the compiler do it for you, ...
you can learn in here, how to write extension method in .Net using both VB & C#
http://www.codeproject.com/Articles/261639/Extension-Methods-in-NET
http://www.codeproject.com/Articles/34295/Extend-the-NET-Library-with-Extension-Methods
只需完全删除 Extension 属性即可 - C# 只需要在第一个参数上使用
this
限定符即可创建扩展方法。Just remove the Extension attribute altogether -- C# only requires the
this
qualifier on the first parameter to create an extension method.没有人真正展示示例语法。
No one actually shows an example syntax.
您正确使用的 C# 的“this”关键字是 [Extension()] 属性的替代品。 删除这些,你就可以开始了。
澄清一下,我所说的“替换”实际上是指“语法糖”——当编译器看到“this”修饰符时,它会生成与您必须在 VB 中手动添加的相同的 ExtensionAttribute。
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.