Поделиться через


AssemblyName Class

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

Describes an assembly's unique identity in full.

Inheritance Hierarchy

System.Object
  System.Reflection.AssemblyName

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

Syntax

'Declaration
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class AssemblyName
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public sealed class AssemblyName

The AssemblyName type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 AssemblyName() Initializes a new instance of the AssemblyName class.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 AssemblyName(String) Initializes a new instance of the AssemblyName class with the specified display name.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 CodeBase Gets or sets the location of the assembly as a URL.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 CultureInfo Gets or sets the culture that is supported by the assembly.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Flags Gets or sets the attributes of the assembly.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 FullName Gets the full name of the assembly, also known as the display name.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 HashAlgorithm Gets or sets the hash algorithm used by the assembly manifest.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Name Gets or sets the simple name of the assembly. This is usually, but not necessarily, the file name of the manifest file of the assembly, minus its extension.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Version Gets or sets the major, minor, build, and revision numbers of the assembly.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 VersionCompatibility Gets or sets information related to the assembly's compatibility with other assemblies.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Clone Makes a copy of this AssemblyName object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetPublicKey Gets the public key that identifies the assembly.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetPublicKeyToken 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.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ReferenceMatchesDefinition Returns a value that indicates whether the loader resolves two assembly names to the same assembly.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 SetPublicKey Sets the public key that identifies the assembly.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 SetPublicKeyToken Sets 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.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns the full name of the assembly, also known as the display name. (Overrides Object.ToString().)

Top

Remarks

The AssemblyName object contains information about an assembly, which you can use to bind to that assembly. An assembly's identity consists of the following:

  • Simple name.

  • Version number.

  • Cryptographic key pair.

  • Supported culture.

The simple name is typically the file name for the manifest file without its extension. The key pair includes a public and private key, used to create strong-name signatures for assemblies.

All compilers that support the common language runtime will emit the simple name of a nested class, and reflection constructs a mangled name when queried, in accordance with the conventions in the following table.

Delimiter

Meaning

Backslash (\)

Escape character.

Comma (,)

Precedes the assembly name.

Plus sign (+)

Precedes a nested class.

For example, the fully qualified name for a class might look like this:

ContainingClass+NestedClass,MyAssembly

A "++" becomes "\+\+", and a "\" becomes "\\".

This qualified name can be persisted and later used to load the Type. To search for and load a Type, use GetType either with the type name only or with the assembly-qualified type name. GetType with the type name only will look for the Type in the caller's assembly and then in the System assembly. GetType with the assembly-qualified type name will look for the Type in any assembly.

A fully specified AssemblyName must have the name, culture, public key or public key token, major version, minor version, build number, and revision number parameters. The last four are packaged in the Version type.

To create a simple name, create an AssemblyName object by using the default constructor and set the Name. The other properties are optional.

To create a full strong name, create an AssemblyName object by using the default constructor, set the Name property, and use the SetPublicKey and SetPublicKeyToken methods to add a public key. The other properties are optional. Use SetPublicKey and SetPublicKeyToken to set the public key and the strong name. The strong name signing always uses the SHA1 hash algorithm.

To ensure that the names are constructed correctly, use the following properties:

For a partially specified strong name, create an AssemblyName object by using the default constructor and set the name and public key. An assembly that is created by using such an AssemblyName can be signed later using the Assembly Linker (Al.exe).

It is possible to specify a public key and a KeyPair with inconsistent values. This can be useful in developer scenarios. In this case, the public key retrieved with GetPublicKey specifies the correct public key, and the KeyPair specifies the public and private keys used during development. When the runtime detects a mismatch between the KeyPair and the public key, it looks up in the registry the correct key that matches the public key.

The format of the display name of an AssemblyName is a comma-delimited Unicode string that begins with the name, as follows:

Name <,Culture = CultureInfo> <,Version = Major.Minor.Build.Revision> <, StrongName> <,PublicKeyToken> '\0'

Name is the textual name of the assembly. CultureInfo is the RFC1766-format-defined culture. Major, Minor, Build, and Revision are the major version, minor version, build number, and revision number of the assembly. StrongName is the hexadecimal-encoded low-order 64 bits of the hash value of the public key generated using the SHA-1 hashing algorithm and the public key specified by SetPublicKey. PublicKeyToken is the hexadecimal-encoded public key specified by SetPublicKey.

Hexadecimal encoding is defined as the conversion of each byte of a binary object to two hexadecimal characters, progressing from least to most significant byte. Additional display values will be added as deemed necessary.

If the full public key is known, PublicKey may be substituted for StrongName.

Also note that except for Name, which must come first, the lexical order of parameters is unimportant. However, any parameter (Version, Culture, StrongName, or PublicKey) that is not specifically set is considered to be omitted, and the AssemblyName is then considered partial. When specifying partial information, Name parameters must be specified in the order described above.

When supplying a display name, the convention StrongName =null or PublicKey= null indicates that binding and matching against a simply named assembly is required. Additionally, the convention Culture= "" (double quote representing an empty string) indicates matching against the default culture.

The following code example shows an AssemblyName for a simply named assembly with default culture.

ExampleAssembly, Culture=""

The following code example shows a fully specified reference for a strongly named assembly with culture "en".

ExampleAssembly, Version=1.0.0.0, Culture=en, PublicKeyToken=a5d015c7d5a0b012

Examples

This section contains two examples. The first example shows how to use the AssemblyName(String) constructor to parse a string that contains a full assembly name. The second example shows how to create an assembly name and use it to define a dynamic assembly.

Example 1

The following example gets the full name of a .NET Framework assembly, parses it by using the AssemblyName(String) constructor, and uses the properties and methods of AssemblyName to display the individual parts.

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
 */

Example 2

The following example creates an AssemblyName and uses it to create a dynamic assembly.

Imports System.Reflection
Imports System.Reflection.Emit

Public Class Example

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

      ' Create an AssemblyName, set its properties, and use it to define a dynamic
      ' assembly.
      Dim aName As New AssemblyName("MyDynamicAssembly")
      aName.CultureInfo = New System.Globalization.CultureInfo("en-US")
      aName.Version = New Version("1.0.0.2001")

      Dim ab As AssemblyBuilder = _
         AppDomain.CurrentDomain.DefineDynamicAssembly(aName, _
                                                       AssemblyBuilderAccess.Run)
      Dim mb As ModuleBuilder = ab.DefineDynamicModule("Temp")
      Dim tb As TypeBuilder = mb.DefineType("Dummy", TypeAttributes.Public)

      Dim t As Type = tb.CreateType()

      outputBlock.Text &= String.Format("Assembly FullName: {0}" & vbLf, _
                                        t.Assembly.FullName) 
   End Sub
End Class

' This code example produces output similar to the following:
'
'Assembly FullName: MyDynamicAssembly, Version=1.0.0.2001, Culture=en-US, PublicKeyToken=null
using System;
using System.Reflection;
using System.Reflection.Emit;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create an AssemblyName, set its properties, and use it to define a dynamic
      // assembly.
      AssemblyName aName = new AssemblyName("MyDynamicAssembly");
      aName.CultureInfo = new System.Globalization.CultureInfo("en-US");
      aName.Version = new Version("1.0.0.2001");

      AssemblyBuilder ab = 
         AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
      ModuleBuilder mb = ab.DefineDynamicModule("Temp");
      TypeBuilder tb = mb.DefineType("Dummy", TypeAttributes.Public);

      Type t = tb.CreateType();

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

/* This code example produces output similar to the following:

Assembly FullName: MyDynamicAssembly, Version=1.0.0.2001, Culture=en-US, PublicKeyToken=null
 */

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.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.