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) 。 标记为受约束区域的代码只能调用具有强可靠性协定的其他代码。 它不应分配或对未准备或不可靠的方法进行虚拟调用,除非它已准备好处理故障。

请注意,在调用 PrepareConstrainedRegions 方法和 try 块之间不允许使用中间语言操作码(除外NOP)。 有关 CER 的详细信息,请参阅 命名空间中的 System.Runtime.ConstrainedExecution 类。

try 块生成 时StackOverflowExceptionPrepareConstrainedRegions使用 方法标记的 CER 不能完美工作。 有关更多信息,请参见 ExecuteCodeWithGuaranteedCleanup 方法。

PrepareConstrainedRegions 方法调用 ProbeForSufficientStack 方法。

适用于