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

注釈

コンパイラは、このメソッドを使用して、、finally、および fault ブロックを制約付き実行領域 (CER) としてマークcatchします。 制約付きリージョンとしてマークされているコードは、強力な信頼性コントラクトを持つ他のコードのみを呼び出す必要があります。 エラーを処理する準備が整っていない限り、準備されていないメソッドまたは信頼性の低いメソッドに対して仮想呼び出しを割り当てたり、仮想呼び出しを行ったりしないでください。

メソッドと try ブロックの呼び出しPrepareConstrainedRegionsの間には、 を除くNOP中間言語オペコードは許可されません。 CER の詳細については、名前空間のクラスに関するページを System.Runtime.ConstrainedExecution 参照してください。

メソッドを使用して PrepareConstrainedRegions マークされた CER は、 がブロックから生成されると StackOverflowException 完全には try 機能しません。 詳細については、ExecuteCodeWithGuaranteedCleanup メソッドを参照してください。

PrepareConstrainedRegions メソッドは ProbeForSufficientStack メソッドを呼び出します。

適用対象