HandleRef-Struktur
Veröffentlicht: Oktober 2016
Umschließt ein verwaltetes Objekt, das ein Handle zu einer Ressource enthält, die über einen Plattformaufruf an nicht verwalteten Code übergeben wird.
Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)
Syntax
[ComVisibleAttribute(true)]
public struct HandleRef
[ComVisibleAttribute(true)]
public value struct HandleRef
[<Sealed>]
[<ComVisibleAttribute(true)>]
type HandleRef = struct end
<ComVisibleAttribute(True)>
Public Structure HandleRef
Konstruktoren
Name | Beschreibung | |
---|---|---|
HandleRef(Object, IntPtr) | Initialisiert eine neue Instanz der HandleRef -Klasse mit dem Objekt auf Wrap und ein Handle auf die Ressource, die von nicht verwaltetem Code verwendet. |
Eigenschaften
Name | Beschreibung | |
---|---|---|
Handle | Ruft das Handle für eine Ressource ab. |
|
Wrapper | Ruft das Objekt, das das Handle für eine Ressource ab. |
Methoden
Name | Beschreibung | |
---|---|---|
Equals(Object) | Gibt an, ob diese Instanz und ein angegebenes Objekt gleich sind.(Geerbt von „ValueType“.) |
|
GetHashCode() | Gibt den Hashcode für diese Instanz zurück.(Geerbt von „ValueType“.) |
|
GetType() | Ruft den Type der aktuellen Instanz ab.(Geerbt von „Object“.) |
|
ToIntPtr(HandleRef) | Gibt die interne ganzzahlige Darstellung der ein HandleRef Objekt. |
|
ToString() | Gibt den voll qualifizierten Typnamen dieser Instanz zurück.(Geerbt von „ValueType“.) |
Operatoren
Name | Beschreibung | |
---|---|---|
Explicit(HandleRef to IntPtr) | Gibt das Handle für eine Ressource des angegebenen HandleRef Objekt. |
Hinweise
Wichtig
Starting with the .NET Framework 2.0, the functionality of the T:System.Runtime.InteropServices.HandleRef class has been replaced by the T:System.Runtime.InteropServices.SafeHandle class and its derived classes, as well as by the T:System.Runtime.InteropServices.CriticalHandle class.
If you use platform invoke to call a managed object, and the object is not referenced elsewhere after the platform invoke call, it is possible for the garbage collector to finalize the managed object. This action releases the resource and invalidates the handle, causing the platform invoke call to fail. Wrapping a handle with T:System.Runtime.InteropServices.HandleRef guarantees that the managed object is not garbage collected until the platform invoke call completes. For a description of platform invoke services, see [<topic://cpconconsumingunmanageddllfunctions>].
The T:System.Runtime.InteropServices.HandleRef value type, like T:System.Runtime.InteropServices.GCHandle, is a special type recognized by the interop marshaler. A normal, nonpinned T:System.Runtime.InteropServices.GCHandle also prevents untimely garbage collection, yet T:System.Runtime.InteropServices.HandleRef provides better performance. Although using T:System.Runtime.InteropServices.HandleRef to keep an object alive for the duration of a platform invoke call is preferred, you can also use the M:System.GC.KeepAlive(System.Object) method for the same purpose.
The T:System.Runtime.InteropServices.HandleRef constructor takes two parameters: an T:System.Object representing the wrapper, and an T:System.IntPtr representing the unmanaged handle. The interop marshaler passes only the handle to unmanaged code, and guarantees that the wrapper (passed as the first parameter to the constructor of the HandleRef) remains alive for the duration of the call.
Versionsinformationen
.NET Framework
Verfügbar seit 1.1
Threadsicherheit
Alle öffentlichen statischen Member ( Shared in Visual Basic) dieses Typs sind threadsicher. Die Threadsicherheit für Instanzmember ist nicht garantiert.
Siehe auch
SafeHandle
CriticalHandle
System.Runtime.InteropServices-Namespace
Zurück zum Anfang