views:

78

answers:

1

While looking at this question and it's answers I thought that it would be a good idea to write an extension method for System.Console that contained the desired functionality.

However, when I tried it, I got this compiler error

System.Console': static types cannot be used as parameters

Here's the code:

using System;
using System.Runtime.CompilerServices;

namespace ConsoleApplication1
{
    public static class ConsoleExtensions
    {
        [Extension]
        public static string TestMethod(this Console console, string testValue)
        {
            return testValue;
        }

    }
}

Is there another way of creating extension methods for static types? Or is this just not possible?

+6  A: 

No it's not possible unfortunately. See http://stackoverflow.com/questions/866921/static-extension-methods

A few people have suggested it: http://madprops.org/blog/static-extension-methods/

...but it was never done in .NET 4. Apparently extension properties got someway to making it but was then abandoned.

https://blogs.msdn.com/ericlippert/archive/2009/10/05/why-no-extension-properties.aspx

Matt Breckon