UnmanagedMemoryStream.CanRead 속성

정의

스트림이 읽기를 지원하는지 여부를 나타내는 값을 가져옵니다.

public:
 virtual property bool CanRead { bool get(); };
public override bool CanRead { get; }
member this.CanRead : bool
Public Overrides ReadOnly Property CanRead As Boolean

속성 값

생성자가 스트림 읽기를 포함하지 않는 access 매개 변수를 사용하여 개체를 만들었고 스트림이 닫혔으면 false이고, 그렇지 않으면 true입니다.

예제

다음 코드 예제에서 읽고 클래스를 사용 하 여 관리 되지 않는 메모리에 쓰는 방법을 보여 줍니다 UnmanagedMemoryStream . 관리되지 않는 메모리 블록은 클래스를 사용하여 Marshal 할당 및 할당 해제됩니다. 이 예제 UnmanagedMemoryStream 에서는 개체가 콘솔에 콘텐츠를 표시하기 전에 속성을 확인하는 CanRead 메서드에 전달됩니다.

// 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.Runtime.InteropServices;
using System.Text;

unsafe class Program
{
    static void Main()
    {
        // Create some data to write.
        byte[] text = UnicodeEncoding.Unicode.GetBytes("Data to write.");

        // Allocate a block of unmanaged memory.
        IntPtr memIntPtr = Marshal.AllocHGlobal(text.Length);

        // Get a byte pointer from the unmanaged memory block.
        byte* memBytePtr = (byte*)memIntPtr.ToPointer();

        UnmanagedMemoryStream writeStream =
            new UnmanagedMemoryStream(
            memBytePtr, text.Length, text.Length, FileAccess.Write);

        // Write the data.
        WriteToStream(writeStream, text);

        // Close the stream.
        writeStream.Close();

        // Create another UnmanagedMemoryStream for reading.
        UnmanagedMemoryStream readStream =
            new UnmanagedMemoryStream(memBytePtr, text.Length);

        // Display the contents of the stream to the console.
        PrintStream(readStream);

        // Close the reading stream.
        readStream.Close();

        // Free up the unmanaged memory.
        Marshal.FreeHGlobal(memIntPtr);
    }

    public static void WriteToStream(UnmanagedMemoryStream writeStream, byte[] text)
    {
        // Verify that the stream is writable:
        // By default, UnmanagedMemoryStream objects do not have write access,
        // write access must be set explicitly.
        if (writeStream.CanWrite)
        {
            // Write the data, byte by byte
            for (int i = 0; i < writeStream.Length; i++)
            {
                writeStream.WriteByte(text[i]);
            }
        }
    }

    public static void PrintStream(UnmanagedMemoryStream readStream)
    {
        byte[] text = new byte[readStream.Length];
        // Verify that the stream is writable:
        // By default, UnmanagedMemoryStream objects do not have write access,
        // write access must be set explicitly.
        if (readStream.CanRead)
        {
            // Write the data, byte by byte
            for (int i = 0; i < readStream.Length; i++)
            {
                text[i] = (byte)readStream.ReadByte();
            }
        }

        Console.WriteLine(UnicodeEncoding.Unicode.GetString(text));
    }
}

설명

이 속성은 현재 스트림 개체가 읽기를 지원하는지 여부를 나타냅니다.

적용 대상