Пример #1
0
        /// <summary>
        /// Create a public key token from a signed assembly
        /// </summary>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="filePath"/> is null
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If <paramref name="filePath"/> is empty
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// If the token could not be generated
        /// </exception>
        /// <param name="filePath">assembly to generate the token of</param>
        /// <returns>public key token of <paramref name="filePath"/></returns>
        public static byte[] CreateTokenFromAssembly(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullOrEmptyException("filePath");
            }

            // get the key and token
            IntPtr blobBuffer  = IntPtr.Zero;
            IntPtr tokenBuffer = IntPtr.Zero;

            try
            {
                int blobSize  = 0;
                int tokenSize = 0;

                // extract the public key token
                if (!StrongNameNative.StrongNameTokenFromAssemblyEx(
                        filePath, out tokenBuffer, out tokenSize, out blobBuffer, out blobSize))
                {
                    Marshal.ThrowExceptionForHR(StrongNameNative.StrongNameErrorInfo());
                }

                // copy the token out of unmanaged memory, and return it
                byte[] token = new byte[tokenSize];
                Marshal.Copy(tokenBuffer, token, 0, tokenSize);
                return(token);
            }
            finally
            {
                if (blobBuffer != IntPtr.Zero)
                {
                    StrongNameNative.StrongNameFreeBuffer(blobBuffer);
                }
                if (tokenBuffer != IntPtr.Zero)
                {
                    StrongNameNative.StrongNameFreeBuffer(tokenBuffer);
                }
            }
        }