有没有什么办法来关闭的StreamWriter而不关闭其BaseStream? [英] Is there any way to close a StreamWriter without closing its BaseStream?

查看:151
本文介绍了有没有什么办法来关闭的StreamWriter而不关闭其BaseStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的根本问题是,当使用要求的Dispose 的StreamWriter ,也部署了 BaseStream (有同样的问题关闭)。

我对这个解决办法,但你可以看到,它涉及复制流。有没有办法做到这一点不复制流?

这样做的目的是为了获得一个字符串(最初从数据库中读取)到一个流的内容,因此,该流可以由第三方组件来读取。结果
NB :我不能改变第三方组件

 公开的System.IO.Stream CreateStream(字符串值)
{
    VAR baseStream =新System.IO.MemoryStream();
    VAR baseCopy =新System.IO.MemoryStream();
    使用(VAR作家=新System.IO.StreamWriter(baseStream,System.Text.Encoding.UTF8))
    {
        writer.Write(值);
        writer.Flush();
        baseStream.WriteTo(baseCopy);
    }
    baseCopy.Seek(0,System.IO.SeekOrigin.Begin);
    返回baseCopy;
}

用于为

 公共无效诺迪()
{
    的System.IO.Stream myStream = CreateStream(此字符串的内容是不重要的);
    My3rdPartyComponent.ReadFromStream(myStream);
}

在理想情况下我在寻找一个名为 BreakAssociationWithBaseStream ,例如

一个假想的方法

 公开的System.IO.Stream CreateStream_Alternate(字符串值)
{
    VAR baseStream =新System.IO.MemoryStream();
    使用(VAR作家=新System.IO.StreamWriter(baseStream,System.Text.Encoding.UTF8))
    {
        writer.Write(值);
        writer.Flush();
        writer.BreakAssociationWithBaseStream();
    }
    返回baseStream;
}


解决方案

如果您使用的.NET Framework 4.5或更高版本,还有的使用的你可以问基本流的StreamWriter超载悬空,当作家是封闭

在更早之前的版本4.5 .NET Framework的,的StreamWriter 假设的它拥有流。选项​​:


  • 请不要处分的StreamWriter ;只是冲洗了。

  • 创建一个流包装它的人一起忽略呼叫关闭 / 的Dispose ,但代理的一切。我有在 MiscUtil 的,如果你想从那里抓住它。
  • 实施

My root problem is that when using calls Dispose on a StreamWriter, it also disposes the BaseStream (same problem with Close).

I have a workaround for this, but as you can see, it involves copying the stream. Is there any way to do this without copying the stream?

The purpose of this is to get the contents of a string (originally read from a database) into a stream, so the stream can be read by a third party component.
NB: I cannot change the third party component.

public System.IO.Stream CreateStream(string value)
{
    var baseStream = new System.IO.MemoryStream();
    var baseCopy = new System.IO.MemoryStream();
    using (var writer = new System.IO.StreamWriter(baseStream, System.Text.Encoding.UTF8))
    {
        writer.Write(value);
        writer.Flush();
        baseStream.WriteTo(baseCopy); 
    }
    baseCopy.Seek(0, System.IO.SeekOrigin.Begin);
    return baseCopy;
}

Used as

public void Noddy()
{
    System.IO.Stream myStream = CreateStream("The contents of this string are unimportant");
    My3rdPartyComponent.ReadFromStream(myStream);
}

Ideally I'm looking for an imaginary method called BreakAssociationWithBaseStream, e.g.

public System.IO.Stream CreateStream_Alternate(string value)
{
    var baseStream = new System.IO.MemoryStream();
    using (var writer = new System.IO.StreamWriter(baseStream, System.Text.Encoding.UTF8))
    {
        writer.Write(value);
        writer.Flush();
        writer.BreakAssociationWithBaseStream();
    }
    return baseStream;
}

解决方案

If you are using .NET Framework 4.5 or later, there is a StreamWriter overload using which you can ask the base stream to be left open when the writer is closed.

In earlier versions of .NET Framework prior to 4.5, StreamWriter assumes it owns the stream. Options:

  • Don't dispose the StreamWriter; just flush it.
  • Create a stream wrapper which ignores calls to Close/Dispose but proxies everything else along. I have an implementation of that in MiscUtil, if you want to grab it from there.

这篇关于有没有什么办法来关闭的StreamWriter而不关闭其BaseStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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