0

Possible Duplicate:
Why aren’t C# static class extension methods supported?

General question:

In Objective-C, I can add a category to any (I think?) class. In that category, I can define class or instance methods.

I know how to make an extension method in .NET that will allow me to implement the same thing as Objective-C category instance methods.

Is there a way to make an extension method that is a class method?

Specific question:

I have created a method that takes a System.DateTime value and returns another System.DateTime value that represents the first of the month that precedes the supplied value. I have turned this into an extension method, and I call it as follows:

Dim dt As DateTime = Now.FirstOfPriorMonth()

However, I only ever call this method on Now, so I'd like to make a class-level extension method and assume Now. Thus, I would like to call the following:

Dim dt As DateTime = System.DateTime.FirstOfLastMonth()

Is this possible? I realize that my currently-working solution is fine, so I'm really more wondering what's possible than trying to "fix" my current problem.

Community
  • 1
  • 1
mbm29414
  • 11,558
  • 6
  • 56
  • 87

1 Answers1

1

I wouldn't get all caught up trying to extend Microsoft's static class: just start your own common date/time utilities class and use that:

Dim dt As DateTime = YourCommonCode.DateTimeUtils.FirstOfLastMonth()
Keith Bluestone
  • 166
  • 2
  • 9