Why am i getting Stream as System.IO.UnmanagedMemoryStream?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 19:09:56

问题


I have a part of code like this

        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());
            }                
        }

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

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


回答1:


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).

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.




回答2:


This is a "by design" behavior and provides a faster access of memory.

Reference: http://msdn2.microsoft.com/en-us/library/system.reflection.emit.modulebuilder.definemanifestresource(VS.85).aspx



来源:https://stackoverflow.com/questions/917589/why-am-i-getting-stream-as-system-io-unmanagedmemorystream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!