Files
Dec2017-Script-Dump/mscorlib/System/Runtime/InteropServices/GCHandle.cs
T
2026-06-04 11:42:34 +02:00

243 lines
13 KiB
C#

using System;
using System.Runtime.CompilerServices;
namespace System.Runtime.InteropServices
{
/// <summary>Provides a means for accessing a managed object from unmanaged memory.</summary>
// Token: 0x02000316 RID: 790
[ComVisible(true)]
[MonoTODO("Struct should be [StructLayout(LayoutKind.Sequential)] but will need to be reordered for that.")]
public struct GCHandle
{
// Token: 0x060021FF RID: 8703 RVA: 0x00079550 File Offset: 0x00077750
private GCHandle(IntPtr h)
{
this.handle = (int)h;
}
// Token: 0x06002200 RID: 8704 RVA: 0x00079560 File Offset: 0x00077760
private GCHandle(object obj)
{
this = new GCHandle(obj, GCHandleType.Normal);
}
// Token: 0x06002201 RID: 8705 RVA: 0x0007956C File Offset: 0x0007776C
private GCHandle(object value, GCHandleType type)
{
if (type < GCHandleType.Weak || type > GCHandleType.Pinned)
{
type = GCHandleType.Normal;
}
this.handle = GCHandle.GetTargetHandle(value, 0, type);
}
/// <summary>Gets a value indicating whether the handle is allocated.</summary>
/// <returns>true if the handle is allocated; otherwise, false.</returns>
// Token: 0x1700064D RID: 1613
// (get) Token: 0x06002202 RID: 8706 RVA: 0x00079590 File Offset: 0x00077790
public bool IsAllocated
{
get
{
return this.handle != 0;
}
}
/// <summary>Gets or sets the object this handle represents.</summary>
/// <returns>The object this handle represents.</returns>
/// <exception cref="T:System.InvalidOperationException">The handle was freed, or never initialized. </exception>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" />
/// </PermissionSet>
// Token: 0x1700064E RID: 1614
// (get) Token: 0x06002203 RID: 8707 RVA: 0x000795A0 File Offset: 0x000777A0
// (set) Token: 0x06002204 RID: 8708 RVA: 0x000795D4 File Offset: 0x000777D4
public object Target
{
get
{
if (!this.IsAllocated)
{
throw new InvalidOperationException(Locale.GetText("Handle is not allocated"));
}
return GCHandle.GetTarget(this.handle);
}
set
{
this.handle = GCHandle.GetTargetHandle(value, this.handle, (GCHandleType)(-1));
}
}
/// <summary>Retrieves the address of an object in a <see cref="F:System.Runtime.InteropServices.GCHandleType.Pinned" /> handle.</summary>
/// <returns>The address of the of the Pinned object as an <see cref="T:System.IntPtr" />.</returns>
/// <exception cref="T:System.InvalidOperationException">The handle is any type other than <see cref="F:System.Runtime.InteropServices.GCHandleType.Pinned" />. </exception>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" />
/// </PermissionSet>
// Token: 0x06002205 RID: 8709 RVA: 0x000795EC File Offset: 0x000777EC
public IntPtr AddrOfPinnedObject()
{
IntPtr addrOfPinnedObject = GCHandle.GetAddrOfPinnedObject(this.handle);
if (addrOfPinnedObject == (IntPtr)(-1))
{
throw new ArgumentException("Object contains non-primitive or non-blittable data.");
}
if (addrOfPinnedObject == (IntPtr)(-2))
{
throw new InvalidOperationException("Handle is not pinned.");
}
return addrOfPinnedObject;
}
/// <summary>Allocates a <see cref="F:System.Runtime.InteropServices.GCHandleType.Normal" /> handle for the specified object.</summary>
/// <returns>A new <see cref="T:System.Runtime.InteropServices.GCHandle" /> that protects the object from garbage collection. This <see cref="T:System.Runtime.InteropServices.GCHandle" /> must be released with <see cref="M:System.Runtime.InteropServices.GCHandle.Free" /> when it is no longer needed.</returns>
/// <param name="value">The object that uses the <see cref="T:System.Runtime.InteropServices.GCHandle" />. </param>
/// <exception cref="T:System.ArgumentException">An instance with nonprimitive (non-blittable) members cannot be pinned. </exception>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" />
/// </PermissionSet>
// Token: 0x06002206 RID: 8710 RVA: 0x00079640 File Offset: 0x00077840
public static GCHandle Alloc(object value)
{
return new GCHandle(value);
}
/// <summary>Allocates a handle of the specified type for the specified object.</summary>
/// <returns>A new <see cref="T:System.Runtime.InteropServices.GCHandle" /> of the specified type. This <see cref="T:System.Runtime.InteropServices.GCHandle" /> must be released with <see cref="M:System.Runtime.InteropServices.GCHandle.Free" /> when it is no longer needed.</returns>
/// <param name="value">The object that uses the <see cref="T:System.Runtime.InteropServices.GCHandle" />. </param>
/// <param name="type">One of the <see cref="T:System.Runtime.InteropServices.GCHandleType" /> values, indicating the type of <see cref="T:System.Runtime.InteropServices.GCHandle" /> to create. </param>
/// <exception cref="T:System.ArgumentException">An instance with nonprimitive (non-blittable) members cannot be pinned. </exception>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" />
/// </PermissionSet>
// Token: 0x06002207 RID: 8711 RVA: 0x00079648 File Offset: 0x00077848
public static GCHandle Alloc(object value, GCHandleType type)
{
return new GCHandle(value, type);
}
/// <summary>Releases a <see cref="T:System.Runtime.InteropServices.GCHandle" />.</summary>
/// <exception cref="T:System.InvalidOperationException">The handle was freed or never initialized. </exception>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" />
/// </PermissionSet>
// Token: 0x06002208 RID: 8712 RVA: 0x00079654 File Offset: 0x00077854
public void Free()
{
GCHandle.FreeHandle(this.handle);
this.handle = 0;
}
// Token: 0x06002209 RID: 8713
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool CheckCurrentDomain(int handle);
// Token: 0x0600220A RID: 8714
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern object GetTarget(int handle);
// Token: 0x0600220B RID: 8715
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern int GetTargetHandle(object obj, int handle, GCHandleType type);
// Token: 0x0600220C RID: 8716
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void FreeHandle(int handle);
// Token: 0x0600220D RID: 8717
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern IntPtr GetAddrOfPinnedObject(int handle);
/// <summary>Determines whether the specified <see cref="T:System.Runtime.InteropServices.GCHandle" /> object is equal to the current <see cref="T:System.Runtime.InteropServices.GCHandle" /> object.</summary>
/// <returns>true if the specified <see cref="T:System.Runtime.InteropServices.GCHandle" /> object is equal to the current <see cref="T:System.Runtime.InteropServices.GCHandle" /> object; otherwise, false.</returns>
/// <param name="o">The <see cref="T:System.Runtime.InteropServices.GCHandle" /> object to compare with the current <see cref="T:System.Runtime.InteropServices.GCHandle" /> object.</param>
// Token: 0x0600220E RID: 8718 RVA: 0x00079668 File Offset: 0x00077868
public override bool Equals(object o)
{
return o != null && o is GCHandle && this.handle == ((GCHandle)o).handle;
}
/// <summary>Returns an identifier for the current <see cref="T:System.Runtime.InteropServices.GCHandle" /> object.</summary>
/// <returns>An identifier for the current <see cref="T:System.Runtime.InteropServices.GCHandle" /> object.</returns>
// Token: 0x0600220F RID: 8719 RVA: 0x000796A0 File Offset: 0x000778A0
public override int GetHashCode()
{
return this.handle.GetHashCode();
}
/// <summary>Returns a new <see cref="T:System.Runtime.InteropServices.GCHandle" /> object created from a handle to a managed object.</summary>
/// <returns>A new <see cref="T:System.Runtime.InteropServices.GCHandle" /> object that corresponds to the value parameter. </returns>
/// <param name="value">An <see cref="T:System.IntPtr" /> handle to a managed object to create a <see cref="T:System.Runtime.InteropServices.GCHandle" /> object from.</param>
/// <exception cref="T:System.InvalidOperationException">The value of the <paramref name="value" /> parameter is <see cref="F:System.IntPtr.Zero" />.</exception>
// Token: 0x06002210 RID: 8720 RVA: 0x000796B0 File Offset: 0x000778B0
public static GCHandle FromIntPtr(IntPtr value)
{
return (GCHandle)value;
}
/// <summary>Returns the internal integer representation of a <see cref="T:System.Runtime.InteropServices.GCHandle" /> object.</summary>
/// <returns>An <see cref="T:System.IntPtr" /> object that represents a <see cref="T:System.Runtime.InteropServices.GCHandle" /> object. </returns>
/// <param name="value">A <see cref="T:System.Runtime.InteropServices.GCHandle" /> object to retrieve an internal integer representation from.</param>
// Token: 0x06002211 RID: 8721 RVA: 0x000796B8 File Offset: 0x000778B8
public static IntPtr ToIntPtr(GCHandle value)
{
return (IntPtr)value;
}
/// <summary>A <see cref="T:System.Runtime.InteropServices.GCHandle" /> is stored using an internal integer representation.</summary>
/// <returns>The integer value.</returns>
/// <param name="value">The <see cref="T:System.Runtime.InteropServices.GCHandle" /> for which the integer is required. </param>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" />
/// </PermissionSet>
// Token: 0x06002212 RID: 8722 RVA: 0x000796C0 File Offset: 0x000778C0
public static explicit operator IntPtr(GCHandle value)
{
return (IntPtr)value.handle;
}
/// <summary>A <see cref="T:System.Runtime.InteropServices.GCHandle" /> is stored using an internal integer representation.</summary>
/// <returns>The <see cref="T:System.Runtime.InteropServices.GCHandle" />.</returns>
/// <param name="value">An <see cref="T:System.IntPtr" /> that indicates the handle for which the conversion is required. </param>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" />
/// </PermissionSet>
// Token: 0x06002213 RID: 8723 RVA: 0x000796D0 File Offset: 0x000778D0
public static explicit operator GCHandle(IntPtr value)
{
if (value == IntPtr.Zero)
{
throw new ArgumentException("GCHandle value cannot be zero");
}
if (!GCHandle.CheckCurrentDomain((int)value))
{
throw new ArgumentException("GCHandle value belongs to a different domain");
}
return new GCHandle(value);
}
/// <summary>Returns a value indicating whether two <see cref="T:System.Runtime.InteropServices.GCHandle" /> objects are equal.</summary>
/// <returns>true if the <paramref name="a" /> and <paramref name="b" /> parameters are equal; otherwise, false.</returns>
/// <param name="a">A <see cref="T:System.Runtime.InteropServices.GCHandle" /> object to compare with the <paramref name="b" /> parameter. </param>
/// <param name="b">A <see cref="T:System.Runtime.InteropServices.GCHandle" /> object to compare with the <paramref name="a" /> parameter. </param>
// Token: 0x06002214 RID: 8724 RVA: 0x0007971C File Offset: 0x0007791C
public static bool operator ==(GCHandle a, GCHandle b)
{
return a.Equals(b);
}
/// <summary>Returns a value indicating whether two <see cref="T:System.Runtime.InteropServices.GCHandle" /> objects are not equal.</summary>
/// <returns>true if the <paramref name="a" /> and <paramref name="b" /> parameters are not equal; otherwise, false.</returns>
/// <param name="a">A <see cref="T:System.Runtime.InteropServices.GCHandle" /> object to compare with the <paramref name="b" /> parameter. </param>
/// <param name="b">A <see cref="T:System.Runtime.InteropServices.GCHandle" /> object to compare with the <paramref name="a" /> parameter. </param>
// Token: 0x06002215 RID: 8725 RVA: 0x0007972C File Offset: 0x0007792C
public static bool operator !=(GCHandle a, GCHandle b)
{
return !a.Equals(b);
}
// Token: 0x04000D3B RID: 3387
private int handle;
}
}