问题描述
我试图重写一个StreamReader的ReadLine方法,但这样做有困难,所以因无法访问一些私有变量。这是可能的,或者我应该只写我自己的StreamReader类?
I'm trying to override a StreamReader's ReadLine method, but having difficulty doing so due to inability to access some private variables. Is this possible, or should I just write my own StreamReader class?
推荐答案
假设您希望自定义的StreamReader是可用的任何地方,一个的TextReader可以用通常有两种选择。
Assuming you want your custom StreamReader to be usable anywhere that a TextReader can be used there are typically two options.
-
从StreamReader的继承和覆盖你想有工作方式不同的功能。在你的情况,这将是StreamReader.ReadLine。
Inherit from StreamReader and override the functions that you want to have work differently. In your case this would be StreamReader.ReadLine.
从TextReader的继承和全面落实读卡器功能添加到您的要求。
Inherit from TextReader and implement the reader functionality completely to your requirements.
注:对于上面的选项2,你可以保持的内部引用一个StreamReader实例,并委派所有功能的内部实例,除了一块要替换功能。在我看来,这只是一个选项2,而不是第三选项的实现细节。
NB: For option 2 above, you can maintain an internal reference to a StreamReader instance and delegate all the functions to the internal instance, except for the piece of functionality that you want to replace. In my view, this is just an implementation detail of option 2 rather than a 3rd option.
根据您的问题,我想你已经尝试选项1,发现重载的StreamReader .ReadLine是相当困难的,因为你不能访问类的内部。那么对于StreamReader的你是幸运的,能做到这一点,而无需访问内部实施的StreamReader的。
Based on your question I assume you have tried option 1 and found that overriding StreamReader.ReadLine is rather difficult because you could not access the internals of the class. Well for StreamReader you are lucky and can achieve this without having access to the internal implementation of the StreamReader.
下面是一个简单的例子:
Here is a simple example:
免责声明:的ReadLine()
实施用于演示目的,并不打算成为一个强大和完整的实施
Disclaimer: The ReadLine()
implementation is for demonstration purposes and is not intended to be a robust or complete implementation.
class CustomStreamReader : StreamReader
{
public CustomStreamReader(Stream stream)
: base(stream)
{
}
public override string ReadLine()
{
int c;
c = Read();
if (c == -1)
{
return null;
}
StringBuilder sb = new StringBuilder();
do
{
char ch = (char)c;
if (ch == ',')
{
return sb.ToString();
}
else
{
sb.Append(ch);
}
} while ((c = Read()) != -1);
return sb.ToString();
}
}
您会发现,我只是用了StreamReader.Read ()方法来读取来自流的字符。而每直接与内部缓冲区的工作共振峰肯定少了,阅读()
方法不使用内部缓冲,因此还是应该得到相当不错的表现,但应该进行测试,以确认。
You will notice that I simply used the StreamReader.Read() method to read the characters from the stream. While definitely less per formant that working directly with the internal buffers, the Read()
method does use the internal buffering so should still yield pretty good performance, but that should be tested to confirm.
有关的乐趣,这里是选项2的例子,我使用了封装的StreamReader,以减少实际的代码,这不是在所有测试..
For fun, here is a example of option 2. I used the encapsulated StreamReader to reduce the actual code, this is not tested at all..
class EncapsulatedReader : TextReader
{
private StreamReader _reader;
public EncapsulatedReader(Stream stream)
{
_reader = new StreamReader(stream);
}
public Stream BaseStream
{
get
{
return _reader.BaseStream;
}
}
public override string ReadLine()
{
int c;
c = Read();
if (c == -1)
{
return null;
}
StringBuilder sb = new StringBuilder();
do
{
char ch = (char)c;
if (ch == ',')
{
return sb.ToString();
}
else
{
sb.Append(ch);
}
} while ((c = Read()) != -1);
return sb.ToString();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_reader.Close();
}
base.Dispose(disposing);
}
public override int Peek()
{
return _reader.Peek();
}
public override int Read()
{
return _reader.Read();
}
public override int Read(char[] buffer, int index, int count)
{
return _reader.Read(buffer, index, count);
}
public override int ReadBlock(char[] buffer, int index, int count)
{
return _reader.ReadBlock(buffer, index, count);
}
public override string ReadToEnd()
{
return _reader.ReadToEnd();
}
public override void Close()
{
_reader.Close();
base.Close();
}
}
这篇关于覆盖的StreamReader的ReadLine方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!