To strong name an assembly using the C# compiler, you need the following:
-
A strong name key pair contained either in a file or in a CSP key container.
-
To use assembly-level attributes to specify the location where the compiler can obtain your strong name key pair.
-
If your key pair is in a file, apply the attribute System.Reflection.AssemblyKeyFileAttribute to your assembly and specify the name of the file that contains the keys.
-
If your key pair is in a CSP container, apply the attribute System.Reflection.AssemblyKeyNameAttribute to your assembly and specify the name of the container in which the keys are stored.
-
Optionally, you can also
-
Specify the culture that your assembly supports by applying the attribute System.Reflection.AssemblyCultureAttribute to the assembly. (You can't specify a culture for executable assemblies because executable assemblies support only the neutral culture.)
-
Specify the version of your assembly by applying the attribute System.Reflection.AssemblyVersionAttribute to the assembly.
The code that follows (from a file named HelloWorld.cs) shows the use of attributes (shown in boldface text) to specify the keys, the culture, and the version for the assembly.
using System;using System.Reflection;[assembly:AssemblyKeyName("MyKeys")][assembly:AssemblyCulture("")][assembly:AssemblyVersion("1.0.0.0")]public class HelloWorld {public static void Main() {Console.WriteLine("Hello, world");}}
To create a strong-named assembly from the example code, create the strong name keys and store them in a file named MyKeyFile using the command sn -k MyKeyFile.snk. Then install the keys into the CSP container named MyKeys using the command sn -i MyKeyFile.snk MyKeys. You can now compile the HelloWorld.cs file into a strong-named assembly using the command csc HelloWorld.cs.
Note: | You can also build strong-named assemblies using the Assembly Linker (al.exe), which allows you to specify the strong name information on the command line instead of using attributes in your code. This is useful when you don't want to embed the strong name attributes in your source file and when you use scripts to build large source trees. Refer to the Assembly Linker information in the .NET Framework SDK documentation for more details. |
No comments:
Post a Comment