Can I add extension methods to an existing static class?

前端 未结 15 1722
慢半拍i
慢半拍i 2020-11-22 07:23

I\'m a fan of extension methods in C#, but haven\'t had any success adding an extension method to a static class, such as Console.

For example, if I want to add an e

相关标签:
15条回答
  • 2020-11-22 07:25

    I stumbled up on this thread while trying to find an answer to the same question the OP had. I didn't find the answer I wanted but I ended up doing this.

    public static class MyConsole
    {
        public static void WriteLine(this ConsoleColor Color, string Text)
        {
            Console.ForegroundColor = Color;
            Console.WriteLine(Text);   
        }
    }
    

    And I use it like this:

    ConsoleColor.Cyan.WriteLine("voilà");
    
    0 讨论(0)
  • 2020-11-22 07:25

    Maybe you could add a static class with your custom namespace and the same class name:

    using CLRConsole = System.Console;
    
    namespace ExtensionMethodsDemo
    {
        public static class Console
        {
            public static void WriteLine(string value)
            {
                CLRConsole.WriteLine(value);
            }
    
            public static void WriteBlueLine(string value)
            {
                System.ConsoleColor currentColor = CLRConsole.ForegroundColor;
    
                CLRConsole.ForegroundColor = System.ConsoleColor.Blue;
                CLRConsole.WriteLine(value);
    
                CLRConsole.ForegroundColor = currentColor;
            }
    
            public static System.ConsoleKeyInfo ReadKey(bool intercept)
            {
                return CLRConsole.ReadKey(intercept);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    Console.WriteBlueLine("This text is blue");   
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.StackTrace);
                }
    
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey(true);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:26

    It is not possible to write an extension method, however it is possible to mimic the behaviour you are asking for.

    using FooConsole = System.Console;
    
    public static class Console
    {
        public static void WriteBlueLine(string text)
        {
            FooConsole.ForegroundColor = ConsoleColor.Blue;
            FooConsole.WriteLine(text);
            FooConsole.ResetColor();
        }
    }
    

    This will allow you to call Console.WriteBlueLine(fooText) in other classes. If the other classes want access to the other static functions of Console, they will have to be explicitly referenced through their namespace.

    You can always add all of the methods in to the replacement class if you want to have all of them in one place.

    So you would have something like

    using FooConsole = System.Console;
    
    public static class Console
    {
        public static void WriteBlueLine(string text)
        {
            FooConsole.ForegroundColor = ConsoleColor.Blue;
            FooConsole.WriteLine(text);
            FooConsole.ResetColor();
        }
        public static void WriteLine(string text)
        {
            FooConsole.WriteLine(text);
        }
    ...etc.
    }
    

    This would provide the kind of behaviour you are looking for.

    *Note Console will have to be added through the namespace that you put it in.

    0 讨论(0)
  • 2020-11-22 07:27

    You can't add static methods to a type. You can only add (pseudo-)instance methods to an instance of a type.

    The point of the this modifier is to tell the C# compiler to pass the instance on the left-side of the . as the first parameter of the static/extension method.

    In the case of adding static methods to a type, there is no instance to pass for the first parameter.

    0 讨论(0)
  • 2020-11-22 07:28

    Nope. Extension method definitions require an instance of the type you're extending. Its unfortunate; I'm not sure why its required...

    0 讨论(0)
  • 2020-11-22 07:28

    I tried to do this with System.Environment back when I was learning extension methods and was not successful. The reason is, as others mention, because extension methods require an instance of the class.

    0 讨论(0)
提交回复
热议问题