2 using System.Runtime.CompilerServices;
3 using System.Runtime.InteropServices;
4 namespace System.Runtime.InteropServices
7 /// Summary description for GCHandle.
12 private IntPtr handle;
13 private GCHandleType handleType;
16 private GCHandle(object obj)
17 : this(obj, GCHandleType.Normal)
20 private GCHandle(object value, GCHandleType type)
28 public bool IsAllocated
32 return (handle != IntPtr.Zero);
40 return GetTarget(handle);
44 SetTarget(handle,value);
49 public IntPtr AddrOfPinnedObject()
51 if(this.handleType == System.Runtime.InteropServices.GCHandleType.Pinned)
53 throw new InvalidOperationException("The handle is not of Pinned type");
55 return GetAddrOfPinnedObject();
58 public static System.Runtime.InteropServices.GCHandle Alloc(object value)
60 return new GCHandle(value);
63 public static System.Runtime.InteropServices.GCHandle Alloc(object value, GCHandleType type)
65 return new GCHandle(value,type);
73 [MethodImplAttribute(MethodImplOptions.InternalCall)]
74 public extern static explicit operator IntPtr(GCHandle value);
75 [MethodImplAttribute(MethodImplOptions.InternalCall)]
76 public extern static explicit operator GCHandle(IntPtr value);
78 //TODO: Private Native Functions
79 [MethodImplAttribute(MethodImplOptions.InternalCall)]
80 private extern object GetTarget(IntPtr pointer);
82 [MethodImplAttribute(MethodImplOptions.InternalCall)]
83 private extern void SetTarget(IntPtr pointer,object obj);
85 [MethodImplAttribute(MethodImplOptions.InternalCall)]
86 private extern void FreeHandle(IntPtr pointer);
88 [MethodImplAttribute(MethodImplOptions.InternalCall)]
89 private extern IntPtr GetAddrOfPinnedObject();