dexter
dexter

Reputation: 7203

Extension methods on a static object

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?

Upvotes: 1

Views: 264

Answers (2)

alexn
alexn

Reputation: 58952

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

Upvotes: 1

Oded
Oded

Reputation: 498904

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.

Upvotes: 4

Related Questions