简体   繁体   English

C#将MemoryStream转换为FileStream

[英]C# Casting MemoryStream to FileStream

My code is this: 我的代码是这样的:

byte[] byteArray = Encoding.ASCII.GetBytes(someText);
MemoryStream stream = new MemoryStream(byteArray);
StreamReader reader = new StreamReader(stream);
FileStream file = (FileStream)reader.BaseStream;

Later I'm using file.Name. 后来我正在使用file.Name。

I'm getting an InvalidCastException: it displays follows 我收到一个InvalidCastException:它显示如下

Unable to cast object of type 'System.IO.MemoryStream' to type 'System.IO.FileStream'. 无法将类型为“System.IO.MemoryStream”的对象强制转换为“System.IO.FileStream”。

I read somewhere that I should just change FileStream to Stream. 我在某处读到了我应该将FileStream更改为Stream。 Is there something else I should do? 还有什么我应该做的吗?

A MemoryStream is not associated with a file, and has no concept of a filename. MemoryStream与文件无关,并且没有文件名的概念。 Basically, you can't do that. 基本上,你不能这样做。

You certainly can't cast between them; 你当然不能在他们之间施放; you can only cast upwards an downwards - not sideways; 你只能向上向上 - 而不是向侧面; to visualise: 可视化:

        Stream
          |
   ---------------
   |             |
FileStream    MemoryStream

You can cast a MemoryStream to a Stream trivially, and a Stream to a MemoryStream via a type-check; 你可以投出MemoryStreamStream平凡,和StreamMemoryStream通过类型检查; but never a FileStream to a MemoryStream . 但从来没有FileStreamMemoryStream That is like saying a dog is an animal, and an elephant is an animal, so we can cast a dog to an elephant. 这就像说狗是动物,大象是动物,所以我们可以把狗交给大象。

You could subclass MemoryStream and add a Name property (that you supply a value for), but there would still be no commonality between a FileStream and a YourCustomMemoryStream , and FileStream doesn't implement a pre-existing interface to get a Name ; 可以 MemoryStream并添加一个Name属性(您为其提供值),但FileStreamYourCustomMemoryStream之间仍然没有共性,而FileStream没有实现预先存在的接口来获取Name ; so the caller would have to explicitly handle both separately, or use duck-typing (maybe via dynamic or reflection). 因此调用者必须单独显式处理,或使用duck-typing(可能通过dynamic或反射)。

Another option (perhaps easier) might be: write your data to a temporary file; 另一种选择(可能更简单)可能是:将数据写入临时文件; use a FileStream from there; 从那里使用FileStream ; then (later) delete the file. 然后(稍后)删除该文件。

You can compare Stream with animal, MemoryStream with dog and FileStream with cat. 您可以将Stream与animal, MemoryStream与dog和FileStream与cat进行比较。 Although a dog is an animal, and a cat is an animal, a dog certainly is not a cat. 虽然狗是动物,而猫是动物,但狗肯定不是猫。

If you want to copy data from one stream to another, you will need to create both streams, read from one and write to the other. 如果要将数据从一个流复制到另一个流,则需要创建两个流,从一个流读取并写入另一个流。

This operation is not possible. 此操作无法进行。 Both FileStream and MemoryStream are directly derived from Stream , so they are sibling types. FileStreamMemoryStream都直接从Stream派生,因此它们是兄弟类型。 In general, in the following scenario: 通常,在以下场景中:

 public class A { }
 public class B : A { }
 public class C : A { }

It is not possible to cast B to C or vice-versa. 无法将B转换为C,反之亦然。 There is no "is-a" relationship between B and C. B和C之间没有“is-a”关系。

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

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