RuntimeHelpers.PrepareConstrainedRegions メソッド
アセンブリ: mscorlib (mscorlib.dll 内)


PrepareConstrainedRegions メソッドは、catch、finally、および fault ブロックを、制約された実行領域 (CER) としてマークします。制約された領域としてマークされたコードから他のコードを呼び出す場合、そのコードには、信頼性が十分高いコントラクトが必要となります。準備されていないメソッドや信頼性の低いメソッドについては、エラー処理に対する準備がなされていないと、メモリを割り当てたり、仮想呼び出しを実行したりすることはできません。
中間言語のオペコード (NOP を除く) を、PrepareConstrainedRegions メソッドの呼び出しと try ブロックの間に置くことはできません。CER の詳細については、System.Runtime.ConstrainedExecution 名前空間の各クラスのトピックを参照してください。
try ブロックから StackOverflowException が生成された場合、PrepareConstrainedRegions メソッドを使ってマークされた CER は完全には機能しません。詳細については、ExecuteCodeWithGuaranteedCleanup メソッドのトピックを参照してください。
PrepareConstrainedRegions メソッドは ProbeForSufficientStack メソッドを呼び出します。

PrepareConstrainedRegions メソッドを使用して、ハンドルを確実に設定する方法を次のコード例に示します。ハンドルを指定した既存のハンドルに確実に設定するには、ネイティブ ハンドルの割り当てと SafeHandle オブジェクト内でのそのハンドルの以降の記録を必ず分離不可能な状態にすることが必要です。これらの操作間でエラーが発生すると (スレッドの中止やメモリ不足の例外によるエラーなど)、ネイティブ ハンドルがリークされることになります。PrepareConstrainedRegions メソッドを使用すると、ハンドルのリークを確実に防ぐことができます。
<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
[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; }

- SecurityPermission (アンマネージ コードを呼び出すために必要なアクセス許可)。UnmanagedCode (関連する列挙体)。LinkDemand (セキュリティ アクション)。

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からRuntimeHelpers.PrepareConstrainedRegions メソッドを検索する場合は、下記のリンクをクリックしてください。

- RuntimeHelpers.PrepareConstrainedRegions メソッドのページへのリンク