Compartir a través de


AssemblyName.GetPublicKeyToken Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the public key token, which is the last 8 bytes of the SHA-1 hash of the public key under which the application or assembly is signed.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function GetPublicKeyToken As Byte()
[SecuritySafeCriticalAttribute]
public byte[] GetPublicKeyToken()

Return Value

Type: array<System.Byte[]
The public key token.

Remarks

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 GetPublicKeyToken returns null instead of byte[0] for a byte[0] PKT or for an assembly name containing PublicKeyToken=null.

Examples

The following example gets the full name of a .NET Framework assembly, parses it by using the AssemblyName(String) constructor, and uses the GetPublicKeyToken method to retrieve and display the public key token.

Imports System.Reflection

Public Class Example
   Private Const mask As Byte = 15

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Use AssemblyName to parse full assembly names. In this example, the 
      ' assembly is mscorlib.dll.
      Dim name As String = GetType(String).Assembly.FullName
      Dim asmName As New AssemblyName(name) 

      outputBlock.Text &= String.Format("Name: {0}" & vbLf, asmName.Name)

      outputBlock.Text &= String.Format("Version: {0}" & vbLf, asmName.Version)

      outputBlock.Text &= String.Format("CultureInfo: {0}" & vbLf, asmName.CultureInfo)

      Dim pkt As New System.Text.StringBuilder()
      For Each b As Byte In asmName.GetPublicKeyToken()
          pkt.Append(Hex(b \ 16 And mask) & Hex(b And mask))
      Next b
      outputBlock.Text &= String.Format("PublicKeyToken: {0}" & vbLf, pkt.ToString())

      outputBlock.Text &= String.Format("FullName: {0}" & vbLf, asmName.FullName)

   End Sub

End Class

' This example produces output similar to the following:
'
'Name: mscorlib
'Version: 2.0.5.0
'CultureInfo: 
'PublicKeyToken: 7CEC85D7BEA7798E
'FullName: mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
using System;
using System.Reflection;

public class Example
{
   private const byte mask = 15;
   private const string hex = "0123456789ABCDEF";

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Use AssemblyName to parse full assembly names. In this example, the 
      // assembly is mscorlib.dll.
      string name = typeof(string).Assembly.FullName;
      AssemblyName asmName = new AssemblyName(name);

      outputBlock.Text += String.Format("Name: {0}\n", asmName.Name);

      outputBlock.Text += String.Format("Version: {0}\n", asmName.Version);

      outputBlock.Text += String.Format("CultureInfo: {0}\n", asmName.CultureInfo);

      System.Text.StringBuilder pkt = new System.Text.StringBuilder();
      foreach( byte b in asmName.GetPublicKeyToken() )
      {
         pkt.Append(hex[b / 16 & mask]);
         pkt.Append(hex[b & mask]);
      }
      outputBlock.Text += String.Format("PublicKeyToken: {0}\n", pkt.ToString());

      outputBlock.Text += String.Format("FullName: {0}\n", asmName.FullName);
   }
}

/* This example produces output similar to the following:

Name: mscorlib
Version: 2.0.5.0
CultureInfo: 
PublicKeyToken: 7CEC85D7BEA7798E
FullName: mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.