1

I know (or so I hear) that writing extension methods for a single stand alone .net class (not an implementation of IEnumerable) is potential code smell. However, for the sake of making the life easier I need to attach a method to the ConfigurationManager class in asp.net. It's a static object so this won't work:

public static List<string> GetSupportedDomains(this ConfigurationManager manager) 
{

     //the manager needs to be static.

}

So the question is - is it possible to write an extension method for a static class in .net?

dexter
  • 7,063
  • 9
  • 54
  • 71
  • Possible duplicate: http://stackoverflow.com/questions/2004416/extension-methods-on-a-static-class – Madhur Ahuja Dec 27 '10 at 15:49
  • Possible duplicate: http://stackoverflow.com/questions/249222/can-i-add-extension-methods-to-an-existing-static-class – Oded Dec 27 '10 at 15:58

2 Answers2

4

No, it isn't possible.

They are defined as static objects that appear to be instance methods.

From MSDN:

Extension methods are defined as static methods but are called by using instance method syntax.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

No you can not. Extension methods require an instance of a given type, which you cannot get from a static class.

alexn
  • 57,867
  • 14
  • 111
  • 145