RuntimeHelpers.PrepareConstrainedRegions 메서드

정의

주의

The Constrained Execution Region (CER) feature is not supported.

코드 본문을 CER(제약이 있는 실행 영역)로 지정합니다.

public:
 static void PrepareConstrainedRegions();
[System.Obsolete("The Constrained Execution Region (CER) feature is not supported.", DiagnosticId="SYSLIB0004", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public static void PrepareConstrainedRegions ();
public static void PrepareConstrainedRegions ();
[System.Security.SecurityCritical]
public static void PrepareConstrainedRegions ();
[<System.Obsolete("The Constrained Execution Region (CER) feature is not supported.", DiagnosticId="SYSLIB0004", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
static member PrepareConstrainedRegions : unit -> unit
static member PrepareConstrainedRegions : unit -> unit
[<System.Security.SecurityCritical>]
static member PrepareConstrainedRegions : unit -> unit
Public Shared Sub PrepareConstrainedRegions ()
특성

예제

다음 예제에서는 메서드를 사용하여 핸들을 안정적으로 설정하는 방법을 PrepareConstrainedRegions 보여줍니다. 핸들을 지정된 기존 핸들로 안정적으로 설정하려면 네이티브 핸들 할당 및 개체 내에서 해당 핸들의 후속 기록이 SafeHandle 원자성인지 확인해야 합니다. 이러한 작업(예: 스레드 중단 또는 메모리 부족 예외) 간의 오류로 인해 네이티브 핸들이 누출됩니다. 메서드를 사용하여 PrepareConstrainedRegions 핸들이 누출되지 않도록 할 수 있습니다.

[StructLayout(LayoutKind.Sequential)]
struct MyStruct
{
    public IntPtr m_outputHandle;
}

sealed class MySafeHandle : SafeHandle
{
    // Called by P/Invoke when returning SafeHandles
    public MySafeHandle()
        : base(IntPtr.Zero, true)
    {
    }

    public MySafeHandle AllocateHandle()
    {
        // Allocate SafeHandle first to avoid failure later.
        MySafeHandle sh = new MySafeHandle();

        RuntimeHelpers.PrepareConstrainedRegions();
        try { }
        finally
        {
            MyStruct myStruct = new MyStruct();
            NativeAllocateHandle(ref myStruct);
            sh.SetHandle(myStruct.m_outputHandle);
        }

        return sh;
    }
<StructLayout(LayoutKind.Sequential)> _
Structure MyStruct
    Public m_outputHandle As IntPtr
End Structure 'MyStruct


NotInheritable Class MySafeHandle
    Inherits SafeHandle

    ' Called by P/Invoke when returning SafeHandles
    Public Sub New()
        MyBase.New(IntPtr.Zero, True)

    End Sub


    Public Function AllocateHandle() As MySafeHandle
        ' Allocate SafeHandle first to avoid failure later.
        Dim sh As New MySafeHandle()

        RuntimeHelpers.PrepareConstrainedRegions()
        Try
        Finally
            Dim myStruct As New MyStruct()
            NativeAllocateHandle(myStruct)
            sh.SetHandle(myStruct.m_outputHandle)
        End Try

        Return sh

    End Function

설명

컴파일러는이 메서드를 사용 하 여 catch , finallyfault 블록을 cer (제약이 있는 실행 영역)로 표시 합니다. 제한된 지역으로 표시된 코드는 강력한 안정성 계약이 있는 다른 코드만 호출해야 합니다. 오류를 처리할 준비가 되지 않은 경우 준비되지 않았거나 불안정한 메서드에 가상 호출을 할당하거나 호출해서는 안 됩니다.

를 제외한 중간 언어 NOP opcode는 메서드 호출과 블록 간에 허용되지 PrepareConstrainedRegions try 않습니다. CER에 대한 자세한 내용은 네임스페이스의 클래스를 System.Runtime.ConstrainedExecution 참조하세요.

메서드를 사용하여 표시된 CER은 PrepareConstrainedRegions 블록에서 이 생성될 때 완벽하게 작동하지 StackOverflowException try 않습니다. 자세한 내용은 ExecuteCodeWithGuaranteedCleanup 메서드를 참조하세요.

PrepareConstrainedRegions 메서드는 ProbeForSufficientStack 메서드를 호출합니다.

적용 대상