문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top