[Serializable] |
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
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. |
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. |
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. |
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:
public AssemblyName(); |
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); } } }
public string CodeBase {get; set;}
|
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
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); } } }
public CultureInfo CultureInfo {get; set;}
|
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); } } }
public string EscapedCodeBase {get;}
|
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
public AssemblyNameFlags Flags {get; set;}
|
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())); } } }
public string FullName {get;}
|
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); } } }
public AssemblyHashAlgorithm HashAlgorithm {get; set;}
|
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); } } }
public StrongNameKeyPair KeyPair {get; set;}
|
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())); } } }
public string Name {get; set;}
|
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); } } }
public Version Version {get; set;}
|
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); } } }
public AssemblyVersionCompatibility VersionCompatibility {get; set;}
|
Currently, VersionCompatibility always returns SameMachine, and is not used by the loader. This property is reserved for a future feature.
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())); } } }
public object Clone(); |
~AssemblyName(); |
public static AssemblyName GetAssemblyName( |
assemblyFile
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. |
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()); } }
public virtual int GetHashCode(); |
public void GetObjectData( |
info
context
Exception Type | Condition |
---|---|
ArgumentNullException | info is null. |
public byte[] GetPublicKey(); |
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())); } } }
public byte[] GetPublicKeyToken(); |
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())); } } }
public Type GetType(); |
protected object MemberwiseClone(); |
public void OnDeserialization( |
sender
public void SetPublicKey( |
publicKey
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)); } } }
public void SetPublicKeyToken( |
publicKeyToken
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)); } } }
public override string ToString(); |
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()); } }