UnmanagedMemoryStream 类
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重要
此 API 不符合 CLS。
提供从托管代码访问非托管内存块的访问权限。
public ref class UnmanagedMemoryStream : System::IO::Stream
public class UnmanagedMemoryStream : System.IO.Stream
[System.CLSCompliant(false)]
public class UnmanagedMemoryStream : System.IO.Stream
type UnmanagedMemoryStream = class
inherit Stream
[<System.CLSCompliant(false)>]
type UnmanagedMemoryStream = class
inherit Stream
Public Class UnmanagedMemoryStream
Inherits Stream
- 继承
- 继承
- 派生
- 属性
下面的代码示例演示如何使用 UnmanagedMemoryStream 类从非托管内存读取和写入。 使用 Marshal 类分配和取消分配非托管内存块。
// Note: you must compile this sample using the unsafe flag.
// From the command line, type the following: csc sample.cs /unsafe
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
unsafe class TestWriter
{
static void Main()
{
// Create some data to read and write.
byte[] message = UnicodeEncoding.Unicode.GetBytes("Here is some data.");
// Allocate a block of unmanaged memory and return an IntPtr object.
IntPtr memIntPtr = Marshal.AllocHGlobal(message.Length);
// Get a byte pointer from the IntPtr object.
byte* memBytePtr = (byte*)memIntPtr.ToPointer();
// Create an UnmanagedMemoryStream object using a pointer to unmanaged memory.
UnmanagedMemoryStream writeStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Write);
// Write the data.
writeStream.Write(message, 0, message.Length);
// Close the stream.
writeStream.Close();
// Create another UnmanagedMemoryStream object using a pointer to unmanaged memory.
UnmanagedMemoryStream readStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Read);
// Create a byte array to hold data from unmanaged memory.
byte[] outMessage = new byte[message.Length];
// Read from unmanaged memory to the byte array.
readStream.Read(outMessage, 0, message.Length);
// Close the stream.
readStream.Close();
// Display the data to the console.
Console.WriteLine(UnicodeEncoding.Unicode.GetString(outMessage));
// Free the block of unmanaged memory.
Marshal.FreeHGlobal(memIntPtr);
Console.ReadLine();
}
}
此类支持使用基于流的现有模型访问非托管内存,并且不需要将非托管内存中的内容复制到堆。
备注
此类型实现 IDisposable 接口,但实际上没有任何要释放的资源。 这意味着不需要直接调用 Dispose() 或使用 using
(在 C# 中)或 Using
(在 Visual Basic 中)等语言构造来释放它。
Unmanaged |
初始化 UnmanagedMemoryStream 类的新实例。 |
Unmanaged |
使用指定的位置、内存长度、总内存量和文件访问值初始化 UnmanagedMemoryStream 类的新实例。 |
Unmanaged |
使用指定的位置和内存长度初始化 UnmanagedMemoryStream 类的新实例。 |
Unmanaged |
使用指定的偏移量、长度和文件访问初始化安全缓冲区中 UnmanagedMemoryStream 类的新实例。 |
Unmanaged |
使用指定的偏移量和长度初始化安全缓冲区中 UnmanagedMemoryStream 类的新实例。 |
Can |
获取一个值,该值指示流是否支持读取。 |
Can |
获取一个值,该值指示流是否支持查找。 |
Can |
获取一个值,该值确定当前流是否可以超时。 (继承自 Stream) |
Can |
获取一个值,该值指示流是否支持写入。 |
Capacity |
获取流长度(大小)或分配给流的内存总量(容量)。 |
Length |
获取流中的数据长度。 |
Position |
获取或设置流中的当前位置。 |
Position |
获取或设置基于流中当前位置的流指向流的字节指针。 |
Read |
获取或设置一个值(以毫秒为单位),该值确定流在超时前尝试读取的时间。 (继承自 Stream) |
Write |
获取或设置一个值(以毫秒为单位),该值确定流在超时之前尝试写入的时间。 (继承自 Stream) |
Copy |
使用取消令牌从 Stream 异步读取字节并将其写入指定的 PipeWriter。 |
As |
将适用于 Windows 应用商店应用的 .NET 中的托管流转换为 Windows 运行时中的输入流。 |
As |
将适用于 Windows 应用商店应用的 .NET 中的托管流转换为 Windows 运行时中的输出流。 |
As |
将指定的流转换为随机访问流。 |
Configure |
配置如何执行从异步可释放项返回的任务的 await。 |
产品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
UWP | 10.0 |