共用方式為


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 ();
[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 ()
屬性

範例

下列範例示範如何使用 方法來可靠地設定句柄 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

備註

編譯程式會使用此方法,將 、 finallyfault 區塊標示catch為限制的執行區域, (CER) 。 標示為受限制區域的程序代碼,只能呼叫具有強可靠性合約的其他程序代碼。 除非準備好處理失敗,否則它不應該配置或對未準備或不可靠的方法進行虛擬呼叫。

請注意,除了 之外 NOP,不會允許中繼語言 opcode,在呼叫 PrepareConstrainedRegions 方法與 try 區塊之間允許。 如需 CER 的詳細資訊,請參閱 命名空間中的 System.Runtime.ConstrainedExecution 類別。

當 從 try 區塊產生 時StackOverflowException,使用 方法標示的 PrepareConstrainedRegions CER 無法正常運作。 如需詳細資訊,請參閱 ExecuteCodeWithGuaranteedCleanup 方法。

PrepareConstrainedRegions 方法會呼叫 ProbeForSufficientStack 方法。

適用於