Can you use an extension method on a type instead of an instance of the type?

I can have an extension method like this:

DateTime d = new DateTime();
d = d.GetRandomDate();

      

GetRandomDate is my extension method. However, the above doesn't make a lot of sense. Which would be better:

DateTime d = DateTime.GetRandomDate();

      

However, I don't know how to do this. Extension method created as:

public static DateTime GetRandomDate(this System.DateTime dt)

      

will only add GetRandomDate () to the first example above, not the second. Is there a way to achieve the desired behavior?

+2


a source to share


3 answers


Why do you need this? If you want to call a static method, why not call it directly?



OK, you need to use something like DateTimeHelper.GetRandomDate()

instead DateTime.GetRandomDate()

.

+2


a source


No impossible



You need to access the method on your own static class ...

+3


a source


Just throwing this away, but could you create a partial static datetime class and throw an extension method at it?

0


a source







All Articles