在查看 Jon 在回答方面做得很好的这个问题时......“如何使用迭代器反向读取文本文件”。还有一个类似的问题,我用指针 hocus pocus 回答了。.net有没有办法在关闭之前从下到上读取文本文件......
现在我确实开始尝试使用指针来解决这个问题,好吧,它看起来很粗糙而且边缘粗糙......
公共类 ReadChar : IEnumerable<char> { 私有流 _strm = null; 私有字符串 _str = string.Empty; 公共读取字符(字符串 s) { this._str = s; } 公共读取字符(流 strm) { this._strm = strm; } 公共 IEnumerator<char> GetEnumerator() { if (this._strm != null && this._strm.CanRead && this._strm.CanSeek) { 返回反向读取流(); } 如果(this._str.Length > 0) { 返回反向读取(); } 返回空值; } 私有 IEnumerator<char> ReverseReadStream() { 长 lIndex = this._strm.Length; 而 (lIndex != 0 && this._strm.Position != 0) { this._strm.Seek(lIndex--, SeekOrigin.End); int nByte = this._strm.ReadByte(); 收益返回 (char)nByte; } } 私有 IEnumerator<char> ReverseRead() { 不安全 { 固定 (char* beg = this._str) { char* p = beg + this._str.Length; 而 (p-- != 乞求) { 收益回报 *p; } } } } IEnumerator IEnumerable.GetEnumerator() { 返回 GetEnumerator(); } }
但发现 C# 编译器无法使用此实现处理此问题,但当 C# 编译器以错误 CS1629 拒绝时被摧毁 - '不安全代码可能不会出现在迭代器中'
为什么呢?