HandleRef 結構

定義

將保有控制代碼的 Managed 物件包裝至要使用平台叫用傳遞至 Unmanaged 程式碼的資源。

public value class HandleRef
public readonly struct HandleRef
public struct HandleRef
[System.Runtime.InteropServices.ComVisible(true)]
public struct HandleRef
type HandleRef = struct
[<System.Runtime.InteropServices.ComVisible(true)>]
type HandleRef = struct
Public Structure HandleRef
繼承
HandleRef
屬性

備註

重要

從 .NET Framework 2.0 開始,類別的功能 HandleRef 已由 SafeHandle 類別及其衍生類別 CriticalHandle 以及 類別取代。

如果您使用平臺叫用來呼叫 Managed 物件,而且在平臺叫用呼叫之後不會參考物件,垃圾收集行程可以完成 Managed 物件。 此動作會釋放資源並使控制碼失效,導致平台叫用呼叫失敗。 包裝控制碼, HandleRef 保證在平臺叫用呼叫完成之前,Managed 物件不會進行垃圾收集。 如需平臺叫用服務的描述,請參閱 取用 Unmanaged DLL 函式

HandleRef 型別,例如 GCHandle ,是 Interop 封送處理器所辨識的特殊類型。 一般、非釘選 GCHandle 也會防止不及時的垃圾收集,但 HandleRef 可提供更佳的效能。 雖然最好在 HandleRef 平臺叫用呼叫期間使用 將物件保持運作,但您也可以針對相同的用途使用 GC.KeepAlive 方法。

HandleRef 構函式採用兩個 Object 參數:代表包裝函式的 ,以及 IntPtr 表示 Unmanaged 控制碼的 。 Interop 封送處理器只會將控制碼傳遞至 Unmanaged 程式碼,並保證包裝函式 (當做第一個參數傳遞至) 的 HandleRef 建構函式,在呼叫期間維持運作狀態。

建構函式

HandleRef(Object, IntPtr)

使用要包裝的物件和 Unmanaged 程式碼所使用的資源控制代碼,初始化 HandleRef 類別的新執行個體。

屬性

Handle

取得資源控制代碼。

Wrapper

取得存有資源控制代碼的物件。

方法

ToIntPtr(HandleRef)

傳回 HandleRef 物件的內部整數表示。

運算子

Explicit(HandleRef to IntPtr)

傳回指定 HandleRef 物件的資源控制代碼。

適用於

另請參閱