RuntimeHelpers.PrepareConstrainedRegions Metoda

Definicja

Przestroga

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

Wyznacza treść kodu jako ograniczony region wykonywania (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 ();
[System.Security.SecurityCritical]
public static void PrepareConstrainedRegions ();
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
[<System.Security.SecurityCritical>]
static member PrepareConstrainedRegions : unit -> unit
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 na określony wstępnie istniejący uchwyt, należy upewnić się, że alokacja uchwytu natywnego i kolejne nagranie tego uchwytu w SafeHandle obiekcie jest niepodzielne. Wszelkie błędy między tymi operacjami (takie jak przerwanie wątku lub wyjątek poza pamięcią) spowodują wyciek uchwytu natywnego. Możesz użyć PrepareConstrainedRegions metody , aby upewnić się, że uchwyt nie wyciekł.

[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 catch, finallyi fault bloków jako ograniczonych regionów wykonywania (CERs). Kod oznaczony jako region ograniczony 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 przygotowana 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 jednostek CERs, zobacz klasy w System.Runtime.ConstrainedExecution przestrzeni nazw.

Ściągnięcia oznaczone przy użyciu PrepareConstrainedRegions metody nie działają idealnie, gdy blok StackOverflowException jest generowany.try Aby uzyskać więcej informacji, zobacz metodę ExecuteCodeWithGuaranteedCleanup .

Metoda PrepareConstrainedRegions wywołuje metodę ProbeForSufficientStack .

Dotyczy