如何将多个TextReader串在一起? [英] How to string multiple TextReaders together?

查看:89
本文介绍了如何将多个TextReader串在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个TextReader-StreamReaders和StringReaders的组合.从概念上讲,它们的串联是单个文本文档.

I have 3 TextReaders -- a combination of StreamReaders and StringReaders. Conceptually, the concatenation of them is a single text document.

我想调用一个采用单个TextReader的方法(不受我的控制).是否有任何内置或简单的方法可以从多个TextReader串联一个TextReader?

I want to call a method (not under my control) that takes a single TextReader. Is there any built-in or easy way to make a concatenating TextReader from multiple TextReaders?

(我可以编写我自己的TextReader子类,但是看起来工作量很大.在那种情况下,我只需要将它们全部写到临时文件中,然后使用一个StreamReader.)

(I could write my own TextReader subclass, but it looks like a fair amount of work. In that case, I'd just write them all out to a temp file and then open it with a single StreamReader.)

有没有一种简单的解决方案可以解决我所缺少的问题?

Is there an easy solution to this that I'm missing?

推荐答案

我只是将它们放在一起,所以它不是超级健壮(没有错误处理等),但是基本的测试用例都可以使用.

I just threw this together, so it's not super-robust (no error handling, etc) but the basic test case works.

它的工作方式是为TextReader创建一个扩展方法,该方法需要花一秒钟,然后返回一个新的TextReader类,该类首先在内部调用Read(),直到用尽,然后再在内部调用Read().第二.您可以无限地链接它.

It works by creating an extension method for TextReader's which take a second, and returns a new TextReader class which internally calls Read() on the first until it runs out, and then starts calling Read()on the second. You can chain this indefinitely.

要提供TextReader的完整实现,您只需实现Read()Peek()Close()Dispose().所有其他方法都依靠特定的实现Read()起作用.因此,创建自己的TextReader确实不是那么糟糕,如下所示.

To provide a complete implementation of TextReader you only need to implement Read(), Peek(), Close() and Dispose(). All the other methods rely on specific implementation Read() to work. So creating your own TextReader really isn't so bad, as you can see below.

这也减轻了任何性能方面的顾虑,因为我们只是包装现有的TextReader而不是实际调用它们来执行串联.

This also alleviates any performance concerns since we are simply wrapping the existing TextReaders and not actually invoking them to perform the concatenation.

class Program
{
    static void Main(string[] args)
    {
        StringReader first = new StringReader("hello ");
        StringReader second = new StringReader("world");
        StringReader third = new StringReader("!");

        using (var allOfThem = first.Concat(second).Concat(third))
        {
            //writes "hello world!"
            Console.WriteLine(allOfThem.ReadToEnd());
        }
        Console.Read();
    }
}

public static class Extensions
{
    public static TextReader Concat(this TextReader first, TextReader second)
    {
        return new ChainedTextReader(first, second);
    }

    private class ChainedTextReader : TextReader
    {
        private TextReader first;
        private TextReader second;
        private bool readFirst = true;

        public ChainedTextReader(TextReader first, TextReader second)
        {
            this.first = first;
            this.second = second;
        }

        public override int Peek()
        {
            if (readFirst)
            {
                return first.Peek();
            }
            else
            {
                return second.Peek();
            }
        }

        public override int Read()
        {
            if (readFirst)
            {
                int value = first.Read();
                if (value == -1)
                {
                    readFirst = false;
                }
                else
                {
                    return value;
                }
            }
            return second.Read();
        }

        public override void Close()
        {
            first.Close();
            second.Close();
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (disposing)
            {
                first.Dispose();
                second.Dispose();
            }
        }
    }
}

这篇关于如何将多个TextReader串在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆