为什么我将Stream作为System

为什么我将Stream作为System

本文介绍了为什么我将Stream作为System.IO.UnmanagedMemoryStream获得?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一部分这样的代码

        Assembly myAssembly = Assembly.GetExecutingAssembly();
        string[] names = myAssembly.GetManifestResourceNames();
        foreach (string file in names)
        {
            if (file.EndsWith(".dtd"))
            {
                // attach to stream to the resource in the manifest
                  dtdStream = myAssembly.GetManifestResourceStream(file);
                  MessageBox.Show(dtdStream.ToString());
            }
        }

我的Resources文件夹下都有所有dtd文件.其构建类型为Embedded Resource.
现在,当我调试代码时,我得到一个显示System.IO.UnmanagedMemoryStream

i have all the dtd files under my Resources folder.Which has build type Embedded Resource.
Now when i Debug the code i am getting a message box showing System.IO.UnmanagedMemoryStream

但是我想要托管的MemoryStream吗?
我的代码有什么问题?
谢谢...

But i want a Managed MemoryStream?
What is wrong in my code?
Thanks...

推荐答案

资源作为程序集(EXE或DLL)的一部分进行编译,这意味着在OS启动进程时将它们加载到非托管内存中.这就是为什么GetManifestResourceStream返回的任何流都必须不受管理的(类型为UnmanagedMemoryStream)的原因.

Resources get compiled as part of the assembly (EXE or DLL), which means they gets loaded into unmanaged memory when the OS starts the process. This is the reason why any stream returned by GetManifestResourceStream must therefore be unmanaged (of type UnmanagedMemoryStream).

这到底是什么问题? MemoryStreamUnmanagedMemoryStream的接口基本上是相同的,只是(隐藏的)功能有所不同,对您没有任何影响.

What's the problem with this, anyway? The interface of MemoryStream and UnmanagedMemoryStream are basically identical, and it's only the (hidden) functionality that differs, which shouldn't be of any consequence to you.

这篇关于为什么我将Stream作为System.IO.UnmanagedMemoryStream获得?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 07:01