Microsoft Visual C#.NET 2003 Kick Start
Any .NET EXE or DLL is an assembly, so as soon as we create either of these items, we've created an assembly. In this chapter's first example, we'll do a little more with assemblies than we have in the past; here, we'll set the assembly's version and title using assembly attributes . Assembly attributes let you set metadata in an assembly, and these attributes are divided into the following types:
Table 13.1 lists the assembly identity attributes, Table 13.2 the informational attributes, Table 13.3 the assembly manifest attributes, and Table 13.4 the strong name attributes. Table 13.1. Assembly Identity Attributes
Table 13.2. Informational Attributes
Table 13.3. Assembly Manifest Attributes
Table 13.4. Strong Name Attributes
You can see our first example in Listing 13.1, where we're setting the assembly's version to 1.0.0.0 and its title to "Example ch13_01" . Note that we're using the System.Reflection namespace here to include the predefined assembly attributes. Listing 13.1 Using Assembly Attributes (ch13_01.cs)
using System; using System.Reflection; [assembly:AssemblyVersionAttribute("1.0.0.0")] [assembly:AssemblyTitleAttribute("Example ch13_01")] class ch13_01 { public static void Main() { System.Console.WriteLine("No worries!"); } } Running ch13_01.exe just displays the text "No worries!" in a console window, but taking a look at the assembly itself is more interesting. To examine the assembly, use the ILDASM tool that comes with Visual Studio, ildasm.exe . (ILDASM is automatically in your path if you use the Visual Studio command prompt. Just select Start, Programs, Microsoft Visual Studio .NET, Visual Studio .NET Tools, Visual Studio .NET Command Prompt.) Use File, Open in this tool to open ch13_01.exe, as you see in Figure 13.1. As you can see in the figure, the Main method appears in our assembly. Double-clicking the MANIFEST entry opens the manifest for the ch13_01 assembly. Figure 13.1. Using ILDASM on an assembly.
Here's what the assembly's manifest looks like. Note in particular that the name of the assembly (given with the .assembly entry) is ch13_01, that we've set the title of the assembly to "Example ch13_01" , and that the assembly's version number (given with the .VER entry) is indeed 1.0.0.0:
.assembly extern mscorlib { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4...ver 1:0:5000:0 } .assembly ch13_01 { .custom instance void [mscorlib]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 0F 45 78 61 6D 70 6C 65 20 63 68 31 33 5F // ...Example ch13_ 30 31 00 00 ) // 01.. // --- The following custom attribute is added automatically, // do not uncomment ------- // .custom instance void [mscorlib] // System.Diagnostics.DebuggableAttribute::.ctor(bool, bool) = // ( 01 00 00 01 00 00 ) .hash algorithm 0x00008004 .ver 1:0:0:0 } .module ch13_01.exe // MVID: {52521874-3C35-485B-B070-EA321722834D} .imagebase 0x00400000 .subsystem 0x00000003 .file alignment 512 .corflags 0x00000001 // Image base: 0x07090000 These kinds of assembly attributes are set routinely in Visual Studio projects in the AssemblyInfo.cs file, created automatically for every project that creates an assembly. For example, here's what AssemblyInfo.cs looks like for the ch12_04 project we saw in the previous chapter:
using System.Reflection; using System.Runtime.CompilerServices; // // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. // [assembly: AssemblyTitle("")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("")] [assembly: AssemblyCopyright("")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // // Version information for an assembly consists of the following four values: // // Major Version // Minor Version // Build Number // Revision // // You can specify all the values or you can default the Revision // and Build Numbers by using the '*' as shown below: [assembly: AssemblyVersion("1.0.*")] // // In order to sign your assembly you must specify a key to use. Refer to the // Microsoft .NET Framework documentation for more // information on assembly signing. // Use the attributes below to control which key is used for signing. // // Notes: // (*) If no key is specified, the assembly is not signed. // (*) KeyName refers to a key that has been installed in the Crypto Service // Provider (CSP) on your machine. KeyFile refers to a file which // contains a key. // (*) If the KeyFile and the KeyName values are both specified, the // following processing occurs: // (1) If the KeyName can be found in the CSP, that key is used. // (2) If the KeyName does not exist and the KeyFile does exist, the key // in the KeyFile is installed into the CSP and used. // (*) In order to create a KeyFile, you can use the // sn.exe (Strong Name) utility. // When specifying the KeyFile, the location of the KeyFile should be // relative to the project output directory which is // %Project Directory%\obj\<configuration>. For example, if your KeyFile // is located in the project directory, you would specify the // AssemblyKeyFile attribute as // [assembly: AssemblyKeyFile("..\..\mykey.snk")] // (*) Delay Signing is an advanced option - see the Microsoft .NET Framework // documentation for more information on this. // [assembly: AssemblyDelaySign(false)] [assembly: AssemblyKeyFile("")] [assembly: AssemblyKeyName("")] |