RuntimeHelpers 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供一組靜態方法和屬性,以提供編譯程序的支援。 無法繼承這個類別。
public ref class RuntimeHelpers abstract sealed
public ref class RuntimeHelpers sealed
public static class RuntimeHelpers
[System.Serializable]
public sealed class RuntimeHelpers
type RuntimeHelpers = class
[<System.Serializable>]
type RuntimeHelpers = class
Public Class RuntimeHelpers
Public NotInheritable Class RuntimeHelpers
- 繼承
-
RuntimeHelpers
- 屬性
範例
下列範例示範如何使用 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
屬性
OffsetToStringData |
已淘汰.
已淘汰.
取得指定字串中數據的位移,以位元組為單位。 |