返回时,MemoryStream禁用读取 [英] MemoryStream disables reading when returned

查看:183
本文介绍了返回时,MemoryStream禁用读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我基本上是在读取文件,对其进行一些处理,然后将其作为内存流传递回主程序,这将由流读取器处理.这一切将由我的主班旁边的一个班级处理.

In my program, I am basically reading in a file, doing some processing to it, and then passing it back to the main program as a memorystream, which will be handled by a streamreader. This will all be handled by a class beside my main.

问题是,当我从另一个类的方法返回内存流时,"canread"变量设置为false,从而导致流读取器初始化失败.

The problem is, when I return the memory stream from my method in another class, the "canread" variable is set to false, and thus causes the streamreader initialization to fail.

下面是发生问题的一个示例(尽管在这里,我正在写另一个类中的内存流,但是当我将其传递回时,它仍然会导致相同的错误.

Below is an example of the problem happening (though in here I'm writing to the memorystream in the other class, but it still causes the same error when i pass it back.

在名为"Otherclass"的类中:

In the class named "Otherclass":

public static MemoryStream ImportantStreamManipulator()
{
   MemoryStream MemStream = new MemoryStream();

   StreamWriter writer = new StreamWriter(MemStream);
   using (writer)
   {
       //Code that writes stuff to the memorystream via streamwriter

       return MemStream;
   }
}

该函数在主程序中调用:

The function calls in the main program:

MemoryStream MStream = Otherclass.ImportantStreamManipulator();
StreamReader reader = new StreamReader(MStream);

当我在"return MemStream"上设置断点时,"CanRead"属性仍设置为true.一旦执行步骤使其返回我的主函数,并将返回值写入MStream,则"CanRead"属性将设置为false.然后,这在StreamReader中导致异常,表明无法读取MStream(如指示的属性所示).数据应该在流缓冲区中,但我无法将其取出.

When I put a breakpoint on the "return MemStream", the "CanRead" property is still set to true. Once I step such that it gets back to my main function, and writes the returned value to MStream, the "CanRead" property is set to false. This then causes an exception in StreamReader saying that MStream could not be read (as the property indicated). The data is in the streams buffer as it should be, but I just can't get it out.

如何设置它,以便一旦"CanRead"返回到我的主设备时它会报告为真?还是我误解了MemoryStream的工作原理以及如何完成我想做的事情?

How do I set it so that "CanRead" will report true once it is returned to my main? Or am I misunderstanding how MemoryStream works and how would I accomplish what I want to do?

推荐答案

这是问题所在

using (writer)
{
    //Code that writes stuff to the memorystream via streamwriter

    return MemStream;
}

您正在关闭编写器,从而关闭了MemoryStream.在这种情况下,您不需要执行此操作...尽管您确实需要 flush 编写器,然后倒带MemoryStream.只需将代码更改为:

You're closing the writer, which closes the MemoryStream. In this case you don't want to do that... although you do need to flush the writer, and rewind the MemoryStream. Just change your code to:

public static MemoryStream ImportantStreamManipulator()
{
   // Probably add a comment here stating that the lack of using statements
   // is deliberate.
   MemoryStream stream = new MemoryStream();

   StreamWriter writer = new StreamWriter(stream);
   // Code that writes stuff to the memorystream via streamwriter

   writer.Flush();
   stream.Position = 0;
   return stream;
}

这篇关于返回时,MemoryStream禁用读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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