可以重载 Console.ReadLine 吗? (或任何静态类方法)

发布于 2024-10-08 18:28:46 字数 625 浏览 6 评论 0原文

我正在尝试创建 System.Console.ReadLine() 方法的重载,该方法将采用字符串参数。我的意图基本上是能够编写,

string s = Console.ReadLine("Please enter a number: ");

Console.Write("Please enter a number: ");
string s = Console.ReadLine();

不是我认为不可能重载 Console.ReadLine 本身,所以我尝试实现一个继承类,如下所示:

public static class MyConsole : System.Console
{
    public static string ReadLine(string s)
    {
        Write(s);
        return ReadLine();
    }
}

那不起作用但是,因为它不可能从 System.Console 继承(因为它是一个静态类,会自动成为一个密封类)。

我在这里尝试做的事情有意义吗?或者从静态类中重载某些东西从来都不是一个好主意?

I'm trying to create an overload of the System.Console.ReadLine() method that will take a string argument. My intention basically is to be able to write

string s = Console.ReadLine("Please enter a number: ");

in stead of

Console.Write("Please enter a number: ");
string s = Console.ReadLine();

I don't think it is possible to overload Console.ReadLine itself, so I tried implementing an inherited class, like this:

public static class MyConsole : System.Console
{
    public static string ReadLine(string s)
    {
        Write(s);
        return ReadLine();
    }
}

That doesn't work though, cause it is not possible to inherit from System.Console (because it is a static class which automatically makes is a sealed class).

Does it make sense what I'm trying to do here? Or is it never a good idea to want to overload something from a static class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2024-10-15 18:28:46

只需将控制台包装在您自己的类中并使用它即可。您不需要为此继承:

class MyConsole
{
    public static string ReadLine()
    {
        return System.Console.ReadLine();
    }
    public static string ReadLine(string message)
    {
        System.Console.WriteLine(message);
        return ReadLine();
    }
    // add whatever other methods you need
}

然后您可以继续并在您的程序中使用它:

string whatEver = MyConsole.ReadLine("Type something useful:");

如果您希望进一步推动它并使 MyConsole 更加灵活,您可以还添加对替换输入/输出实现的支持:

class MyConsole
{
    private static TextReader reader = System.Console.In;
    private static TextWriter writer = System.Console.Out;

    public static void SetReader(TextReader reader)
    {
        if (reader == null)
        {
            throw new ArgumentNullException("reader");
        }
        MyConsole.reader = reader;
    }

    public static void SetWriter(TextWriter writer)
    {
        if (writer == null)
        {
            throw new ArgumentNullException("writer");
        }
        MyConsole.writer = writer;
    }


    public static string ReadLine()
    {
        return reader.ReadLine();
    }
    public static string ReadLine(string message)
    {

        writer.WriteLine(message);
        return ReadLine();
    }
    // and so on
}

这将允许您从任何 TextReader 实现驱动程序,因此命令可以来自文件而不是控制台,这可以提供一些不错的自动化场景...

更新
您需要公开的大多数方法都非常简单。好吧,写起来可能有点乏味,但这不需要很多分钟的工作,而且你只需要做一次。

示例(假设我们在上面的第二个示例中,具有可分配的读取器和写入器):

public static void WriteLine()
{
    writer.WriteLine();
}

public static void WriteLine(string text)
{
    writer.WriteLine(text);
}

public static void WriteLine(string format, params object args)
{
    writer.WriteLine(format, args);
}

Just wrap the console in your own class and use that instead. You don't need to inherit for that:

class MyConsole
{
    public static string ReadLine()
    {
        return System.Console.ReadLine();
    }
    public static string ReadLine(string message)
    {
        System.Console.WriteLine(message);
        return ReadLine();
    }
    // add whatever other methods you need
}

Then you can go ahead and use that one in your program instead:

string whatEver = MyConsole.ReadLine("Type something useful:");

If you wish to push it a bit further and make MyConsole a bit more flexible, you could also add support to replace the input/output implementations:

class MyConsole
{
    private static TextReader reader = System.Console.In;
    private static TextWriter writer = System.Console.Out;

    public static void SetReader(TextReader reader)
    {
        if (reader == null)
        {
            throw new ArgumentNullException("reader");
        }
        MyConsole.reader = reader;
    }

    public static void SetWriter(TextWriter writer)
    {
        if (writer == null)
        {
            throw new ArgumentNullException("writer");
        }
        MyConsole.writer = writer;
    }


    public static string ReadLine()
    {
        return reader.ReadLine();
    }
    public static string ReadLine(string message)
    {

        writer.WriteLine(message);
        return ReadLine();
    }
    // and so on
}

This would allow you to drive the program from any TextReader implementation, so commands could come from a file instead of the console, which could provide some nice automation scenarios...

Update
Most of the methods that you need to expose are extremely simple. OK, a bit tedious to write perhaps, but it's not many minutes of work, and you need to do it only once.

Example (assuming we are in my second sample above, with assignable reader and writer):

public static void WriteLine()
{
    writer.WriteLine();
}

public static void WriteLine(string text)
{
    writer.WriteLine(text);
}

public static void WriteLine(string format, params object args)
{
    writer.WriteLine(format, args);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文