RuntimeHelpers.PrepareConstrainedRegions Metoda

Definicja

Wyznacza treść kodu jako ograniczony region wykonywania (CER).

public:
 static void PrepareConstrainedRegions();
public static void PrepareConstrainedRegions();
[System.Security.SecurityCritical]
public static void PrepareConstrainedRegions();
static member PrepareConstrainedRegions : unit -> unit
[<System.Security.SecurityCritical>]
static member PrepareConstrainedRegions : unit -> unit
Public Shared Sub PrepareConstrainedRegions ()
Atrybuty

Przykłady

W poniższym przykładzie pokazano, jak niezawodnie ustawić uchwyty przy użyciu PrepareConstrainedRegions metody . Aby niezawodnie ustawić uchwyt do określonego wstępnie istniejącego uchwytu, należy upewnić się, że alokacja uchwytu natywnego i kolejne rejestrowanie tego uchwytu w SafeHandle obiekcie jest niepodzielne. Wszelkie błędy między tymi operacjami (takie jak przerwanie wątku lub wyjątek braku pamięci) spowodują wyciek natywnego dojścia. Możesz użyć PrepareConstrainedRegions metody , aby upewnić się, że dojście nie wyciekło.

[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

Uwagi

Kompilatory używają tej metody do oznaczania catchwartości , finallyi fault bloków jako ograniczonych regionów wykonywania (CERs). Kod oznaczony jako ograniczony region musi wywoływać tylko inny kod z silnymi kontraktami niezawodności. Nie należy przydzielać ani wykonywać wywołań wirtualnych do nieprzygotowanych lub zawodnych metod, chyba że jest on przygotowany do obsługi błędów.

Należy pamiętać, że żadne kody operacyjne języka pośredniego, z wyjątkiem NOP, są dozwolone między wywołaniem PrepareConstrainedRegions metody a blokiem try . Aby uzyskać więcej informacji na temat ceRs, zobacz klasy w System.Runtime.ConstrainedExecution przestrzeni nazw.

CeRs, które są oznaczone przy użyciu PrepareConstrainedRegions metody, nie działają idealnie, gdy element StackOverflowException jest generowany z try bloku. Aby uzyskać więcej informacji, zobacz metodę ExecuteCodeWithGuaranteedCleanup.

Metoda PrepareConstrainedRegions wywołuje metodę ProbeForSufficientStack .

Dotyczy