A MemoryStream not associated with a file and has no concept of a file name. In principle, you cannot do this.
You cannot throw between them; you can only throw up and down - not sideways; for visualization:
Stream | --------------- | | FileStream MemoryStream
You can make a MemoryStream to Stream trivial and Stream to MemoryStream using type checking; but never FileStream until MemoryStream . It is like a dog is an animal and an elephant is an animal, so we can throw the dog to an elephant.
You can subclass MemoryStream and add the Name property (which you specify), but there will be no commonality between FileStream and a YourCustomMemoryStream , and FileStream will not implement the existing interface to get Name ; therefore, the caller will have to explicitly process both separately and use duck print (possibly through dynamic or reflection).
Another option (perhaps easier) could be: writing your data to a temporary file; use FileStream from there; then (later) delete the file.
source share