How do you create an extension in C#? -


I wrote this extension in VB.NET for stringbuilder:

  import system . Compiler services public module sbExtension & lt; Extension () & gt; _ Public Sub-AppendFormattedLine (as ByVal oStr System.Text.StringBuilder, _ ByVal as the format string, _ ByVal as the arg0 object) oStr.AppendFormat ("{0} {1}", String.Format (format , Arg0), ControlChars.Newline) and all sub lieutenant; Extension () & gt; _ Public Sub-AppendFormattedLine (as byVal oStr System.Text.StringBuilder, _ ByVal format string, as byVal arg0 object, as in the form of _ ByVal ARG1 object) oStr.AppendFormat ("{0} {1} ", String.Format (Format, Arg0, arg1), Control Chairs.NewLine) and Sub End Lt; Extension () & gt; _ Public Sub-AppendFormattedLine (ByVal oStr System.Text.StringBuilder, _ ByVal as the format string, _ ByVal as the arg0 object, _ ByVal as the ARG1 object, _ ByVal as ARG2 object) as oStr. AppendFormat ("{0} {1}", string.format (format, arg 0, arg1, arg2), control chars.Newline) and sub end lt; Extension () & gt; _ Public Sub AppendFormattedLine (ByVal oStr as System.Text.StringBuilder, _ ByVal as the format string, _ ByVal as ParamArray Arg () object) oStr.AppendFormat ("{0} {1}", String Format (format, args), control chairs.New line) and sub and module  

I tried to port it to c # (I gradually myself got C # I'm learning), but I've failed this way:

  Use the system. time. Complementary Services; 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. New line); } [Extension ()] public static void AppendFormattedLine (The System.Text.StringBuilder oStr, string format, string arg0, string ARG1) {oStr.AppendFormat ("{0} {1}", String.Format (format, arg0, Arg1), Environmental New Line); } [Extension ()] Public static zero formatted redline (this system. StringBilder OST, string format, string RGR, string AGRA, string RG2) {oStr.AppendFormat ("{0} {1}", string.format Format, arg0, arg1, arg2), environment. New line); } [Extension ()] Public static zero formatted format (this system.textBringerOST, string format, string [] AGR) {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: 'System. Do not use. Compiler services Extension attribute 'instead of' this' keyword.

I have uncertainty about what this means, because if I change '[extension]' with 'this', then this is the only thing related to an extension' Extension Attribute ', but that does not work too.

Do anyone know what I am missing?

Thank you!

The keyword "C" of C # which you are using correctly, [extension ( )] Is the replacement for the attribute. Remove them and you'll love to go

To make it clear by "replacement", I actually mean "syntax sugar" - when the compiler sees "this" modifier, then it generates the same extension extension in VB. Add to


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -