public struct GCHandle
Object
ValueType
GCHandle
mscorlib
RuntimeInfrastructure
Provides a means for referencing a managed object from unmanaged memory.
Use a GCHandle when an object reference is required to be accessible from unmanaged memory.The GCHandleType enumeration describes the possible GCHandle types.
[Note: If the type of the GCHandle is System.Runtime.InteropServices.GCHandleType.Normal, then it is an opaque handle, and the address of the object it references cannot be resolved through it.]
System.Runtime.InteropServices Namespace
GCHandle Methods
GCHandle.AddrOfPinnedObject Method
GCHandle.Alloc(System.Object) Method
GCHandle.Alloc(System.Object, System.Runtime.InteropServices.GCHandleType) Method
GCHandle.Free Method
System.Runtime.InteropServices.GCHandle GCHandle.op_Explicit(System.IntPtr) Method
System.IntPtr GCHandle.op_Explicit(System.Runtime.InteropServices.GCHandle) Method
GCHandle Properties
public IntPtr AddrOfPinnedObject();
Returns the address of an object being referred to by a System.Runtime.InteropServices.GCHandleType.Pinned handle.
A IntPtr containing the address of the of the System.Runtime.InteropServices.GCHandleType.Pinned object as a IntPtr .
Exception Type Condition InvalidOperationException The handle type is not System.Runtime.InteropServices.GCHandleType.Pinned .
System.Runtime.InteropServices.GCHandle Structure, System.Runtime.InteropServices Namespace
public static GCHandle Alloc(object value);
Allocates a System.Runtime.InteropServices.GCHandleType.Normal handle for the specified object.
- value
- The object for which the GCHandle is created. Can be
null
.
A new GCHandle instance that protects the object from garbage collection.
A System.Runtime.InteropServices.GCHandleType.Normal handle ensures the object will not be collected by the garbage collector.If the value parameter is
null
, this method returns a valid GCHandle. [Note: The target of the handle may be changed via the System.Runtime.InteropServices.GCHandle.Target property.]
System.Runtime.InteropServices.GCHandle Structure, System.Runtime.InteropServices Namespace
public static GCHandle Alloc(object value, GCHandleType type);
Allocates a handle of the specified type for the specified object.
- value
- The object for which the GCHandle is created. Can be
null
.- type
- A GCHandleType value that specifies the type of GCHandle to create.
A new GCHandle instance that protects the object.
If the value parameter isnull
, this method returns a valid GCHandle. [Note: The target of the handle may be changed via the System.Runtime.InteropServices.GCHandle.Target property.]
System.Runtime.InteropServices.GCHandle Structure, System.Runtime.InteropServices Namespace
public void Free();
Frees a GCHandle.
Exception Type Condition InvalidOperationException The handle has already been freed or was never initialized.
The caller is required to provide synchronization to prevent multiple threads from executing this method simultaneously for a given handle.
System.Runtime.InteropServices.GCHandle Structure, System.Runtime.InteropServices Namespace
public static explicit operator GCHandle(IntPtr value);
Convert a IntPtr to a GCHandle instance.
- value
- The IntPtr to be converted.
A GCHandle .
GCHandle instances are stored using an internal integer representation. This method allows you to retrieve a GCHandle from its integer representation.
System.Runtime.InteropServices.GCHandle Structure, System.Runtime.InteropServices Namespace
public static explicit operator IntPtr(GCHandle value);
Converts a GCHandle instance to a IntPtr.
- value
- The GCHandle to be converted.
A IntPtr representation of the specified GCHandle .
GCHandle instances are stored using an internal integer representation. This method allows you to retrieve that representation.
System.Runtime.InteropServices.GCHandle Structure, System.Runtime.InteropServices Namespace
public bool IsAllocated { get; }
Gets a Boolean value indicating whether the current GCHandle instance is allocated.
true
if the handle is allocated; otherwise,false
.
This property is read-only.Use this method to determine whether the GCHandle is still available.
[Note: When the garbage collector collects the object, the handle could still be resurrected in the finalizer.]
System.Runtime.InteropServices.GCHandle Structure, System.Runtime.InteropServices Namespace
public object Target { get; set; }
Gets or sets a reference to the object the current GCHandle instance represents.
The object this handle represents.
Exception Type Condition InvalidOperationException The current GCHandle instance has already been freed or was never initialized.
System.Runtime.InteropServices.GCHandle Structure, System.Runtime.InteropServices Namespace