以尽可能最小的努力从 StreamWriter 继承

发布于 2024-08-21 18:58:19 字数 154 浏览 5 评论 0原文

我正在使用允许使用 StreamWriter 进行日志记录的外部库 - 现在我想根据日志记录的内容添加一些处理。由于我想避免循环遍历日志文件,因此我想编写一个继承自 StreamWriter 的类。
从 StreamWriter 继承并尽可能少地重新实现方法/构造函数的最佳方法是什么?

I am using an external library which allows logging using StreamWriter - now I want to add some handling based on the content of the logging. As I want to avoid to loop through the log file, I would like to write a class which inherits from StreamWriter.
What is the best way to inherit from StreamWriter with as few re-implementations of methods/constructors?

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

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

发布评论

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

评论(2

情何以堪。 2024-08-28 18:58:19

我不确定你到底想做什么,但如果你只想检查流中写入的内容,你可以这样做:

public class CustomStreamWriter : StreamWriter
{
    public CustomStreamWriter(Stream stream)
        : base(stream)
    {}

    public override void Write(string value)
    {
        //Inspect the value and do something

        base.Write(value);
    }
}

I'm not sure of what you want to do exactly, but if you only want to inspect what is being written in the stream, you can do this:

public class CustomStreamWriter : StreamWriter
{
    public CustomStreamWriter(Stream stream)
        : base(stream)
    {}

    public override void Write(string value)
    {
        //Inspect the value and do something

        base.Write(value);
    }
}
回梦 2024-08-28 18:58:19

确定外部库使用的构造函数并实现该构造函数(或仅实现全部),然后您只需重写外部库使用的写入方法即可。

public class Class1 : StreamWriter 
{
    public Class1(Stream stream)
        : base(stream)
    {

    }
    public Class1(Stream stream, Encoding encoding)
        : base(stream, encoding)
    {

    }
    public Class1(Stream stream, Encoding encoding, int bufferSize)
        : base(stream, encoding, bufferSize)
    {

    }
    public Class1(string path)
        : base(path)
    {

    }
    public Class1(string path, bool append)
        : base(path, append)
    {

    }
    public Class1(string path, bool append, Encoding encoding)
        : base(path, append, encoding)
    {

    }
    public Class1(string path, bool append, Encoding encoding, int bufferSize)
        : base(path, append, encoding, bufferSize)
    {

    }

    public override void Write(string value)
    {
        base.Write(value);
    }
}

Determine the constructor the external library use and implement that (or just implement them all) and then you just need to override the write method(s) that your external library uses.

public class Class1 : StreamWriter 
{
    public Class1(Stream stream)
        : base(stream)
    {

    }
    public Class1(Stream stream, Encoding encoding)
        : base(stream, encoding)
    {

    }
    public Class1(Stream stream, Encoding encoding, int bufferSize)
        : base(stream, encoding, bufferSize)
    {

    }
    public Class1(string path)
        : base(path)
    {

    }
    public Class1(string path, bool append)
        : base(path, append)
    {

    }
    public Class1(string path, bool append, Encoding encoding)
        : base(path, append, encoding)
    {

    }
    public Class1(string path, bool append, Encoding encoding, int bufferSize)
        : base(path, append, encoding, bufferSize)
    {

    }

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