System.Reflection.AssemblyName Class

Assembly: Mscorlib.dll
Namespace: System.Reflection
Summary
Fully describes an assembly's unique identity.
C# Syntax:
[Serializable]
public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback
Remarks
The assembly cache manager uses AssemblyName objects for binding and retrieving information about an assembly. An assembly's identity consists of a simple name, a version number, a cryptographic key pair, and supported culture. The simple name is unencrypted name, as distinguished from the strong name. The strong name is an assembly name secured with a public and private cryptographic key pair.

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 following conventions.



Delimiter Meaning
Backslash (\) Escape character.
Comma (,) Precedes the Assembly name.
Plus sign (+) Precedes a nested class.
Period (.) Denotes namespace identifiers.

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

TopNamespace.SubNameSpace.ContainingClass+NestedClass,MyAssembly

If the namespace were TopNamespace.Sub+Namespace, then the string would have to precede the plus sign (+) with an escape character (\) to prevent it from being interpreted as a nesting separator. Reflection emits this string as follows:

TopNamespace.Sub\+Namespace.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 Type.GetType either with the type name only or with the assembly qualified type name. Type.GetType with the type name only will look for the Type in the caller's assembly and then in the System assembly. Type.GetType with the assembly qualified type name will look for the Type in any assembly.

Type names may include trailing characters that denote additional information about the type, such as whether the type is a reference type, a pointer type or an array type. To retrieve the type name without these trailing characters, use t.GetElementType().ToString() , where t is the type.

Spaces are relevant in all type name components except the assembly name. In the assembly name, spaces before the ',' separator are relevant, but spaces after the ',' separator are ignored.

A fully specified AssemblyName must have, in this order, the name, culture, originator, major version, minor version, build number, and revision number parameters. The last four are packaged in the Version type. However, an AssemblyName may specify a partial query when an insufficient number of parameters is supplied to explicitly determine a unique assembly. The assembly cache manager returns the first assembly that matches the specified AssemblyName. The parameter order for partial assembly name references must be the order specified above. For example, you may specify and match on the name, culture, and originator with the version parameters omitted, but you may not match on the name and originator, omitting both the culture and the version. When used for binding, the name is the minimum requirement.

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

To create a full strong name, create an AssemblyName object using the default constructor and set the AssemblyName.Name and AssemblyName.KeyPair. The other properties are optional. Use AssemblyName.SetPublicKey and AssemblyName.SetPublicKeyToken to set the public key and the strong name. The strong name signing always uses the System.Security.Cryptography.SHA1 (not supported on the shared source CLI) hash algorithm.

For a partial specified strong name, create an AssemblyName object using the default constructor and set the name and public key. The other properties are optional. An assembly created using such an AssemblyName can be signed later using the Assembly Generation tool (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 gotten with AssemblyName.GetPublicKey specifies the correct public key, while 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 the SetPublicKey.

Quoted values are optional. 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, then 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 ) 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.

com.microsoft.crypto, Culture=""

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

com.microsoft.crypto, Culture=en, PublicKeyToken=a5d015c7d5a0b012, Version=1.0.0.0

The following code examples each show a partially specified AssemblyName, which can be satisfied by either a strong or a simply named assembly.

com.microsoft.crypto

com.microsoft.crypto, Culture=""

com.microsoft.crypto, Culture=en

The following code examples each show a partially specified AssemblyName, which must be satisfied by a simply named assembly.

com.microsoft.crypto, Culture="", PublicKeyToken=null

com.microsoft.crypto, Culture=en, PublicKeyToken=null

The following code examples each show a partially specified AssemblyName, which must be satisfied by a strongly named assembly.

com.microsoft.crypto, Culture="", PublicKeyToken=a5d015c7d5a0b012

com.microsoft.crypto, Culture=en, PublicKeyToken=a5d015c7d5a0b012, Version=1.0.0.0

See also:
System.Reflection Namespace | Version | AssemblyName.GetPublicKey | AssemblyName.GetPublicKeyToken | AssemblyName.SetPublicKey | AssemblyName.SetPublicKeyToken | Type.FullName | Type.GetType | Type.AssemblyQualifiedName | MSDN: specifyingfullyqualifiedtypenames

System.Reflection.AssemblyName Member List:

Public Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the AssemblyName class.
Public Properties
CodeBase Read-write

Gets or sets the location of the assembly as a URL.
CultureInfo Read-write

Gets or sets the culture supported by the assembly.
EscapedCodeBase Read-only

Gets the URI, including escape characters, that represents the codebase.
Flags Read-write

Gets or sets the attributes of the assembly.
FullName Read-only

Gets the full name of the assembly, also known as the display name.
HashAlgorithm Read-write

Gets or sets the hash algorithm used by the assembly manifest.
KeyPair Read-write

Gets or sets the public and private cryptographic key pair generated by the originator of the assembly.
Name Read-write

Gets or sets the simple, unencrypted name of the assembly.
Version Read-write

Gets or sets the major, minor, revision, and build numbers of the assembly.
VersionCompatibility Read-write

Gets or sets the information related to the assembly's compatibility with other assemblies.
Public Methods
Clone Makes a copy of this AssemblyName object.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetAssemblyName Gets the AssemblyName for a given file.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetObjectData Gets serialization information with all of the data needed to reinstantiate this AssemblyName.
GetPublicKey Gets the public key identifying the originator of the assembly.
GetPublicKeyToken Gets a strong name consisting of a public key, a given name, and version parts.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
OnDeserialization Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete.
SetPublicKey Sets the public key identifying the originator of the assembly.
SetPublicKeyToken Sets a strong name consisting of a public key, a given name, and version parts.
ToString Overridden:
Returns the full name of the assembly, also known as the display name.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Reflection.AssemblyName Member Details

ctor #1
Summary
Initializes a new instance of the AssemblyName class.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public AssemblyName();
Remarks

using System;
using System.Reflection;
using System.Threading;
using System.Reflection.Emit;

public class AssemblyName_Constructor
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {


      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName(); 
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");

      // Get all the assemblies currently loaded in the application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'. 
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
      {
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];
      }
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the assembly name\n");
         Console.WriteLine(myAssembly);
      }
   }
}

    

Return to top


Property: CodeBase (read-write)
Summary
Gets or sets the location of the assembly as a URL.
C# Syntax:
public string CodeBase {get; set;}
Exceptions
Exception Type Condition
SecurityException The caller does not have the required permission.
Example

using System;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Globalization;
using System.Reflection.Emit;
using System.Configuration.Assemblies;

public class AssemblyName_CodeBase
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {



      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName();
      // Set the codebase to the physical directory were the assembly resides.
      myAssemblyName.CodeBase = String.Concat("file:///", Directory.GetCurrentDirectory());
      // Set the culture information of the assembly to 'English-American'.
      myAssemblyName.CultureInfo = new CultureInfo("en-US");
      // Set the hash algoritm to 'SHA1'.
      myAssemblyName.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");

      // Get all the assemblies currently loaded in the application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'. 
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
      {
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];
      }
      // Display the full assembly information to the console.
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the full assembly name\n");
         Console.WriteLine(myAssembly.GetName().FullName);
      }
   }
}

    
.NET Framework Security:
FileIOPermission for access to the path. Associated enumeration: FileIOPermissionAccess.PathDiscovery.

Return to top


Property: CultureInfo (read-write)
Summary
Gets or sets the culture supported by the assembly.
C# Syntax:
public CultureInfo CultureInfo {get; set;}
Example

using System;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Globalization;
using System.Reflection.Emit;
using System.Configuration.Assemblies;

public class AssemblyName_CodeBase
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {



      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName();
      // Set the codebase to the physical directory were the assembly resides.
      myAssemblyName.CodeBase = String.Concat("file:///", Directory.GetCurrentDirectory());
      // Set the culture information of the assembly to 'English-American'.
      myAssemblyName.CultureInfo = new CultureInfo("en-US");
      // Set the hash algoritm to 'SHA1'.
      myAssemblyName.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");

      // Get all the assemblies currently loaded in the application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'. 
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
      {
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];
      }
      // Display the full assembly information to the console.
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the full assembly name\n");
         Console.WriteLine(myAssembly.GetName().FullName);
      }
   }
}

    

Return to top


Property: EscapedCodeBase (read-only)
Summary
Gets the URI, including escape characters, that represents the codebase.
C# Syntax:
public string EscapedCodeBase {get;}
Exceptions
Exception Type Condition
SecurityException The caller does not have the required permission.
.NET Framework Security:
FileIOPermission for access to the path. Associated enumeration: FileIOPermissionAccess.PathDiscovery.

Return to top


Property: Flags (read-write)
Summary
Gets or sets the attributes of the assembly.
C# Syntax:
public AssemblyNameFlags Flags {get; set;}
Example

using System;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Globalization;
using System.Reflection.Emit;
using System.Configuration.Assemblies;
using System.Text;

public class AssemblyName_CodeBase
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {
      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName();
      // Set the codebase to the physical directory were the assembly resides.
      myAssemblyName.CodeBase = String.Concat("file:///", Directory.GetCurrentDirectory());
      // Set the culture information of the assembly to 'English-American'.
      myAssemblyName.CultureInfo = new CultureInfo("en-US");
      // Set the hash algoritm to 'SHA1'.
      myAssemblyName.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
      myAssemblyName.VersionCompatibility = AssemblyVersionCompatibility.SameProcess;
      myAssemblyName.Flags = AssemblyNameFlags.PublicKey;
      // Provide this assembly with a strong name.
      myAssemblyName.KeyPair = new StrongNameKeyPair(File.Open("KeyPair.snk", FileMode.Open));
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");
      
      // Get the assemblies loaded in the current application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'. 
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];

      // Display the full assembly information to the console.
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the full assembly name\n");
         Console.WriteLine(myAssembly.GetName().FullName);
         Console.WriteLine("\nDisplaying the public key\n");
         Console.WriteLine(Encoding.ASCII.GetString(myAssembly.GetName().GetPublicKey()));
         Console.WriteLine("\nDisplaying the public key token\n");
         Console.WriteLine(Encoding.ASCII.GetString(myAssembly.GetName().GetPublicKeyToken()));
      }
   }
}

    

Return to top


Property: FullName (read-only)
Summary
Gets the full name of the assembly, also known as the display name.
C# Syntax:
public string FullName {get;}
Example

using System;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Globalization;
using System.Reflection.Emit;
using System.Configuration.Assemblies;

public class AssemblyName_CodeBase
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {



      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName();
      // Set the codebase to the physical directory were the assembly resides.
      myAssemblyName.CodeBase = String.Concat("file:///", Directory.GetCurrentDirectory());
      // Set the culture information of the assembly to 'English-American'.
      myAssemblyName.CultureInfo = new CultureInfo("en-US");
      // Set the hash algoritm to 'SHA1'.
      myAssemblyName.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");

      // Get all the assemblies currently loaded in the application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'. 
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
      {
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];
      }
      // Display the full assembly information to the console.
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the full assembly name\n");
         Console.WriteLine(myAssembly.GetName().FullName);
      }
   }
}

    
See also:
MSDN: specifyingfullyqualifiedtypenames

Return to top


Property: HashAlgorithm (read-write)
Summary
Gets or sets the hash algorithm used by the assembly manifest.
C# Syntax:
public AssemblyHashAlgorithm HashAlgorithm {get; set;}
Example

using System;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Globalization;
using System.Reflection.Emit;
using System.Configuration.Assemblies;

public class AssemblyName_CodeBase
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {



      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName();
      // Set the codebase to the physical directory were the assembly resides.
      myAssemblyName.CodeBase = String.Concat("file:///", Directory.GetCurrentDirectory());
      // Set the culture information of the assembly to 'English-American'.
      myAssemblyName.CultureInfo = new CultureInfo("en-US");
      // Set the hash algoritm to 'SHA1'.
      myAssemblyName.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");

      // Get all the assemblies currently loaded in the application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'. 
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
      {
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];
      }
      // Display the full assembly information to the console.
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the full assembly name\n");
         Console.WriteLine(myAssembly.GetName().FullName);
      }
   }
}

    

Return to top


Property: KeyPair (read-write)
Summary
Gets or sets the public and private cryptographic key pair generated by the originator of the assembly.
C# Syntax:
public StrongNameKeyPair KeyPair {get; set;}
Remarks
When the runtime loads an assembly, it does not set the AssemblyName.KeyPair property. The getter for the property is only useful if the user set the property after the assembly was loaded and wants to subsequently retrieve the property.
Example

using System;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Globalization;
using System.Reflection.Emit;
using System.Configuration.Assemblies;
using System.Text;

public class AssemblyName_CodeBase
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {
      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName();
      // Set the codebase to the physical directory were the assembly resides.
      myAssemblyName.CodeBase = String.Concat("file:///", Directory.GetCurrentDirectory());
      // Set the culture information of the assembly to 'English-American'.
      myAssemblyName.CultureInfo = new CultureInfo("en-US");
      // Set the hash algoritm to 'SHA1'.
      myAssemblyName.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
      myAssemblyName.VersionCompatibility = AssemblyVersionCompatibility.SameProcess;
      myAssemblyName.Flags = AssemblyNameFlags.PublicKey;
      // Provide this assembly with a strong name.
      myAssemblyName.KeyPair = new StrongNameKeyPair(File.Open("KeyPair.snk", FileMode.Open));
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");
      
      // Get the assemblies loaded in the current application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'. 
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];

      // Display the full assembly information to the console.
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the full assembly name\n");
         Console.WriteLine(myAssembly.GetName().FullName);
         Console.WriteLine("\nDisplaying the public key\n");
         Console.WriteLine(Encoding.ASCII.GetString(myAssembly.GetName().GetPublicKey()));
         Console.WriteLine("\nDisplaying the public key token\n");
         Console.WriteLine(Encoding.ASCII.GetString(myAssembly.GetName().GetPublicKeyToken()));
      }
   }
}

    

Return to top


Property: Name (read-write)
Summary
Gets or sets the simple, unencrypted name of the assembly.
C# Syntax:
public string Name {get; set;}
Remarks
Assembly names are limited to the number of characters as defined by MAX_PATH.
Example

using System;
using System.Reflection;
using System.Threading;
using System.Reflection.Emit;

public class AssemblyName_Constructor
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {


      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName(); 
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");

      // Get all the assemblies currently loaded in the application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'. 
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
      {
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];
      }
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the assembly name\n");
         Console.WriteLine(myAssembly);
      }
   }
}

    
See also:
MSDN: specifyingfullyqualifiedtypenames

Return to top


Property: Version (read-write)
Summary
Gets or sets the major, minor, revision, and build numbers of the assembly.
C# Syntax:
public Version Version {get; set;}
Example

using System;
using System.Reflection;
using System.Threading;
using System.Reflection.Emit;

public class AssemblyName_Constructor
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {


      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName(); 
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");

      // Get all the assemblies currently loaded in the application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'. 
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
      {
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];
      }
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the assembly name\n");
         Console.WriteLine(myAssembly);
      }
   }
}

    

Return to top


Property: VersionCompatibility (read-write)
Summary
Gets or sets the information related to the assembly's compatibility with other assemblies.
C# Syntax:
public AssemblyVersionCompatibility VersionCompatibility {get; set;}
Remarks
VersionCompatibility information indicates, for example, that the assembly cannot execute side-by-side with other versions due to conflicts over a device driver.

Currently, VersionCompatibility always returns SameMachine, and is not used by the loader. This property is reserved for a future feature.

Example

using System;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Globalization;
using System.Reflection.Emit;
using System.Configuration.Assemblies;
using System.Text;

public class AssemblyName_CodeBase
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {
      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName();
      // Set the codebase to the physical directory were the assembly resides.
      myAssemblyName.CodeBase = String.Concat("file:///", Directory.GetCurrentDirectory());
      // Set the culture information of the assembly to 'English-American'.
      myAssemblyName.CultureInfo = new CultureInfo("en-US");
      // Set the hash algoritm to 'SHA1'.
      myAssemblyName.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
      myAssemblyName.VersionCompatibility = AssemblyVersionCompatibility.SameProcess;
      myAssemblyName.Flags = AssemblyNameFlags.PublicKey;
      // Provide this assembly with a strong name.
      myAssemblyName.KeyPair = new StrongNameKeyPair(File.Open("KeyPair.snk", FileMode.Open));
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");
      
      // Get the assemblies loaded in the current application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'. 
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];

      // Display the full assembly information to the console.
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the full assembly name\n");
         Console.WriteLine(myAssembly.GetName().FullName);
         Console.WriteLine("\nDisplaying the public key\n");
         Console.WriteLine(Encoding.ASCII.GetString(myAssembly.GetName().GetPublicKey()));
         Console.WriteLine("\nDisplaying the public key token\n");
         Console.WriteLine(Encoding.ASCII.GetString(myAssembly.GetName().GetPublicKeyToken()));
      }
   }
}

    

Return to top


Method: Clone()
Summary
Makes a copy of this AssemblyName object.
C# Syntax:
public object Clone();
Return Value:
An Object that is a copy of this AssemblyName object.
Implements:
ICloneable.Clone
Remarks
A new object is created, identical to the original.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~AssemblyName();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetAssemblyName(
   string assemblyFile
)
Summary
Gets the AssemblyName for a given file.
C# Syntax:
public static AssemblyName GetAssemblyName(
   string assemblyFile
);
Parameters:

assemblyFile

The assembly file for which to get the AssemblyName.

Return Value:
An AssemblyName object representing the given file.
Exceptions
Exception Type Condition
ArgumentNullException assemblyFile is null.
ArgumentException assemblyFile is empty.
FileNotFoundException assemblyFile is not found.
SecurityException The caller does not have path discovery permission.
FileLoadException An assembly or module was loaded twice with two different evidences.
BadImageFormatException assemblyFile is not a valid assembly.
Remarks
This will only work if the file contains an assembly manifest. This method causes the file to be opened and closed, but the assembly is not added to this domain.
Example

using System;
using System.Reflection;
using System.Text.RegularExpressions;

public class AssemblyName_GetAssemblyName
{
   public static void Main()
   {


      // Get the type of 'System.Object'.
      Type myType = typeof(System.Object);
      
      // Get the path of 'System.dll'.
      string system = Regex.Replace(myType.Assembly.CodeBase, "mscorlib.dll", "System.dll");
      system = Regex.Replace(system, "file:///", "");

      // Get the assembly information and display to the console.
      AssemblyName myAssemblyName = AssemblyName.GetAssemblyName(system);
      Console.WriteLine("\nDisplaying the assembly information of 'System.dll'\n");
      Console.WriteLine(myAssemblyName.ToString());
   }
}

    
.NET Framework Security:
FileIOPermission for access to information in the path. Associated enumeration: FileIOPermissionAccess.PathDiscovery.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetObjectData(
   SerializationInfo info,
   StreamingContext context
)
Summary
Gets serialization information with all of the data needed to reinstantiate this AssemblyName.
C# Syntax:
public void GetObjectData(
   SerializationInfo info,
   StreamingContext context
);
Parameters:

info

The object to be populated with serialization information.

context

The destination context of the serialization.

Exceptions
Exception Type Condition
ArgumentNullException info is null.
Implements:
ISerializable.GetObjectData

Return to top


Method: GetPublicKey()
Summary
Gets the public key identifying the originator of the assembly.
C# Syntax:
public byte[] GetPublicKey();
Return Value:
An array of type byte containing the public key of the assembly originator.
Example

using System;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Globalization;
using System.Reflection.Emit;
using System.Configuration.Assemblies;
using System.Text;

public class AssemblyName_CodeBase
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {
      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName();
      // Set the codebase to the physical directory were the assembly resides.
      myAssemblyName.CodeBase = String.Concat("file:///", Directory.GetCurrentDirectory());
      // Set the culture information of the assembly to 'English-American'.
      myAssemblyName.CultureInfo = new CultureInfo("en-US");
      // Set the hash algoritm to 'SHA1'.
      myAssemblyName.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
      myAssemblyName.VersionCompatibility = AssemblyVersionCompatibility.SameProcess;
      myAssemblyName.Flags = AssemblyNameFlags.PublicKey;
      // Provide this assembly with a strong name.
      myAssemblyName.KeyPair = new StrongNameKeyPair(File.Open("KeyPair.snk", FileMode.Open));
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");
      
      // Get the assemblies loaded in the current application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'. 
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];

      // Display the full assembly information to the console.
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the full assembly name\n");
         Console.WriteLine(myAssembly.GetName().FullName);
         Console.WriteLine("\nDisplaying the public key\n");
         Console.WriteLine(Encoding.ASCII.GetString(myAssembly.GetName().GetPublicKey()));
         Console.WriteLine("\nDisplaying the public key token\n");
         Console.WriteLine(Encoding.ASCII.GetString(myAssembly.GetName().GetPublicKeyToken()));
      }
   }
}

    

Return to top


Method: GetPublicKeyToken()
Summary
Gets a strong name consisting of a public key, a given name, and version parts.
C# Syntax:
public byte[] GetPublicKeyToken();
Return Value:
An array of type byte containing the components of the strong name.
Example

using System;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Globalization;
using System.Reflection.Emit;
using System.Configuration.Assemblies;
using System.Text;

public class AssemblyName_CodeBase
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {
      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName();
      // Set the codebase to the physical directory were the assembly resides.
      myAssemblyName.CodeBase = String.Concat("file:///", Directory.GetCurrentDirectory());
      // Set the culture information of the assembly to 'English-American'.
      myAssemblyName.CultureInfo = new CultureInfo("en-US");
      // Set the hash algoritm to 'SHA1'.
      myAssemblyName.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
      myAssemblyName.VersionCompatibility = AssemblyVersionCompatibility.SameProcess;
      myAssemblyName.Flags = AssemblyNameFlags.PublicKey;
      // Provide this assembly with a strong name.
      myAssemblyName.KeyPair = new StrongNameKeyPair(File.Open("KeyPair.snk", FileMode.Open));
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");
      
      // Get the assemblies loaded in the current application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'. 
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];

      // Display the full assembly information to the console.
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the full assembly name\n");
         Console.WriteLine(myAssembly.GetName().FullName);
         Console.WriteLine("\nDisplaying the public key\n");
         Console.WriteLine(Encoding.ASCII.GetString(myAssembly.GetName().GetPublicKey()));
         Console.WriteLine("\nDisplaying the public key token\n");
         Console.WriteLine(Encoding.ASCII.GetString(myAssembly.GetName().GetPublicKeyToken()));
      }
   }
}

    

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: OnDeserialization(
   object sender
)
Summary
Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete.
C# Syntax:
public void OnDeserialization(
   object sender
);
Parameters:

sender

The source of the deserialization event.

Implements:
IDeserializationCallback.OnDeserialization
See also:
ISerializable | AssemblyName.GetObjectData

Return to top


Method: SetPublicKey(
   byte[] publicKey
)
Summary
Sets the public key identifying the originator of the assembly.
C# Syntax:
public void SetPublicKey(
   byte[] publicKey
);
Parameters:

publicKey

A byte array containing the public key identifying the originator of the assembly.

Example
using System;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Globalization;
using System.Reflection.Emit;
using System.Configuration.Assemblies;
using System.Text;

public class AssemblyName_CodeBase
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {
      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName();
      // Set the codebase to the physical directory were the assembly resides.
      myAssemblyName.CodeBase = String.Concat("file:///", Directory.GetCurrentDirectory());
      // Set the culture information of the assembly to 'English-American'.
      myAssemblyName.CultureInfo = new CultureInfo("en-US");
      // Set the hash algoritm to 'SHA1'.
      myAssemblyName.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
      myAssemblyName.VersionCompatibility = AssemblyVersionCompatibility.SameProcess;
      myAssemblyName.Flags = AssemblyNameFlags.PublicKey;
      // Get the whole contents of the 'PublicKey.snk' into a byte array.
      FileStream publicKeyStream = File.Open("PublicKey.snk", FileMode.Open);
      byte[] publicKey = new byte[publicKeyStream.Length];
      publicKeyStream.Read(publicKey, 0, (int)publicKeyStream.Length);
      // Provide the assembly with a public key.
      myAssemblyName.SetPublicKey(publicKey);
      // Get the whole contents of the 'PublicKeyToken.snk' into a byte array.
      FileStream publicKeyTokenStream = File.Open("PublicKeyToken.snk", FileMode.Open);
      byte[] publicKeyToken = new Byte[publicKeyTokenStream.Length];
      publicKeyTokenStream.Read(publicKeyToken, 0, (int)publicKeyToken.Length);
      // Provide the assembly with a public key token.
      myAssemblyName.SetPublicKeyToken(publicKeyToken);
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");
      
      // Get the assemblies loaded in the current application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'. 
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];
  
      // Display the full assembly information to the console.
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the full assembly name\n");
         string assemblyName = myAssembly.GetName().FullName;
         Console.WriteLine(assemblyName);
         Console.WriteLine("\nDisplaying the public key for the assembly\n");
         byte[] publicKeyBytes = myAssembly.GetName().GetPublicKey();
         Console.WriteLine(Encoding.ASCII.GetString(publicKeyBytes));
         Console.WriteLine("\nDisplaying the public key token for the assembly\n");
         byte[] publicKeyTokenBytes = myAssembly.GetName().GetPublicKeyToken();
         Console.WriteLine(Encoding.ASCII.GetString(publicKeyTokenBytes));
      }
   }
}

    

Return to top


Method: SetPublicKeyToken(
   byte[] publicKeyToken
)
Summary
Sets a strong name consisting of a public key, a given name, and version parts.
C# Syntax:
public void SetPublicKeyToken(
   byte[] publicKeyToken
);
Parameters:

publicKeyToken

A byte array containing the strong name of the assembly.

Example
using System;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Globalization;
using System.Reflection.Emit;
using System.Configuration.Assemblies;
using System.Text;

public class AssemblyName_CodeBase
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {
      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName();
      // Set the codebase to the physical directory were the assembly resides.
      myAssemblyName.CodeBase = String.Concat("file:///", Directory.GetCurrentDirectory());
      // Set the culture information of the assembly to 'English-American'.
      myAssemblyName.CultureInfo = new CultureInfo("en-US");
      // Set the hash algoritm to 'SHA1'.
      myAssemblyName.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
      myAssemblyName.VersionCompatibility = AssemblyVersionCompatibility.SameProcess;
      myAssemblyName.Flags = AssemblyNameFlags.PublicKey;
      // Get the whole contents of the 'PublicKey.snk' into a byte array.
      FileStream publicKeyStream = File.Open("PublicKey.snk", FileMode.Open);
      byte[] publicKey = new byte[publicKeyStream.Length];
      publicKeyStream.Read(publicKey, 0, (int)publicKeyStream.Length);
      // Provide the assembly with a public key.
      myAssemblyName.SetPublicKey(publicKey);
      // Get the whole contents of the 'PublicKeyToken.snk' into a byte array.
      FileStream publicKeyTokenStream = File.Open("PublicKeyToken.snk", FileMode.Open);
      byte[] publicKeyToken = new Byte[publicKeyTokenStream.Length];
      publicKeyTokenStream.Read(publicKeyToken, 0, (int)publicKeyToken.Length);
      // Provide the assembly with a public key token.
      myAssemblyName.SetPublicKeyToken(publicKeyToken);
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");
      
      // Get the assemblies loaded in the current application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'. 
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];
  
      // Display the full assembly information to the console.
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the full assembly name\n");
         string assemblyName = myAssembly.GetName().FullName;
         Console.WriteLine(assemblyName);
         Console.WriteLine("\nDisplaying the public key for the assembly\n");
         byte[] publicKeyBytes = myAssembly.GetName().GetPublicKey();
         Console.WriteLine(Encoding.ASCII.GetString(publicKeyBytes));
         Console.WriteLine("\nDisplaying the public key token for the assembly\n");
         byte[] publicKeyTokenBytes = myAssembly.GetName().GetPublicKeyToken();
         Console.WriteLine(Encoding.ASCII.GetString(publicKeyTokenBytes));
      }
   }
}

    

Return to top


Overridden Method: ToString()
Summary
Returns the full name of the assembly, also known as the display name.
C# Syntax:
public override string ToString();
Return Value:
A String that is the full name of the assembly, or the class name if the full name of the assembly cannot be determined.
Remarks
See the description of AssemblyName for the format of the returned string.
Example

using System;
using System.Reflection;
using System.Text.RegularExpressions;

public class AssemblyName_GetAssemblyName
{
   public static void Main()
   {


      // Get the type of 'System.Object'.
      Type myType = typeof(System.Object);
      
      // Get the path of 'System.dll'.
      string system = Regex.Replace(myType.Assembly.CodeBase, "mscorlib.dll", "System.dll");
      system = Regex.Replace(system, "file:///", "");

      // Get the assembly information and display to the console.
      AssemblyName myAssemblyName = AssemblyName.GetAssemblyName(system);
      Console.WriteLine("\nDisplaying the assembly information of 'System.dll'\n");
      Console.WriteLine(myAssemblyName.ToString());
   }
}

    

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.