简体   繁体   English

将MemoryStream对象传递给FileStream对象

[英]Pass a MemoryStream object to a FileStream object

I have a MemoryStream object, and i want to write it to a file and pass it as a parameter in the following passFile method. 我有一个MemoryStream对象,我想将其写入文件并在以下passFile方法passFile其作为参数传递。 The passFile method currently accepts a FileStream object as a parameter, but is there a way where i could convert the MemoryStream object to a FileStream object? passFile方法当前接受FileStream对象作为参数,但是有没有一种方法可以将MemoryStream对象转换为FileStream对象? Help 救命

MemoryStream ms = new MemoryStream();

public void passFile (FileStream file){

}

No, you cannot cast it: MemoryStream and FileStream are siblings in the inheritance hierarchy, they cannot be cast to one another. 不,您不能强制转换: MemoryStreamFileStream是继承层次结构中的兄弟姐妹,它们不能互相强制转换。

If the method cannot be re-written to take a Stream , you could write the content of the MemoryStream into a temporary file , and then open a FileStream based on the content of that file. 如果无法重写该方法以获取Stream ,则可以MemoryStream的内容写入一个临时文件中 ,然后根据该文件的内容打开一个FileStream

You could use a Stream abstraction, and pass any type of stream as FileStream , MemoryStream , etc... for sample: 您可以使用Stream抽象,并将任何类型的流作为FileStreamMemoryStream等传递给示例:

public void passFile(Stream stream)
{
   // process stream
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM