How to write a method extension for a static Class in VB.NET?

621 views Asked by At

I would like to add a custom WriteColoredText method to the Console Class, to acces like this:

Console.WriteColoredText(Parametters)

The problem is that I only found information about creating method extensions for specific datatypes but any about a method extension for a Class.

If this is not possibly, which similar alternatives I have?

1

There are 1 answers

4
p.s.w.g On BEST ANSWER

The short answer is, you can't.

Extension methods can only apply to object instances, but the Console class is static. The only solution is to write your own, completely separate class, say ConsoleEx, and implement any methods you want there.

Extension methods make it possible to write a method that can be called as if it were an instance method of the existing type.