コード例 #1
0
        /// <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>
        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);
        }