我是c#中扩展方法的粉丝,但还没有成功地将扩展方法添加到静态类中,比如Console
。
例如,如果我想添加一个扩展到Console
,称为“WriteBlueLine
”,这样我就可以:
Console.WriteBlueLine("This text is blue");
我尝试通过添加一个本地,公共静态方法,用Console
作为“this
”参数…但是不行!
public static class Helpers {
public static void WriteBlueLine(this Console c, string text)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine(text);
Console.ResetColor();
}
}
这没有添加一个“WriteBlueLine
”方法到Console
…我做错了吗?或者要求不可能的事情?