What Is an Attribute?


It's difficult to define an attribute in a single sentence — it's best to learn by examining how they are used. For now I'll define an attribute as extra information that can be applied to chunks of code within an assembly — such as a class, method, or property. This information is accessible to any other class that uses the assembly.

Chapter 26 discussed assemblies and mentioned the AssemblyInfo.cs file. Let's have a look at this file — create a new Windows application called AttributePeek in the C:\BegVCSharp\Chapter27 folder, and open the Solution Explorer. Expand the Properties node, as shown in Figure 27-1.

image from book
Figure 27-1

If you double-click on this file, and you'll see some code created by Visual Studio. Part of this code is shown here:

 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("AttributePeek")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("MorganSkinner.com")] [assembly: AssemblyProduct("AttributePeek")] [assembly: AssemblyCopyright("Copyright MorganSkinner.com 2005")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] 

For brevity, only part of the file is shown here. Within this file there are a number of lines beginning [assembly: — these are attribute definitions. When the file is compiled, any attributes defined are saved into the resulting assembly — this process is known as pickling. To see this in action, modify one of the preceding attributes — say the AssemblyTitle attribute — and compile your assembly:

 [assembly: AssemblyTitle("AttributePeek – by ME")] 

Once it is compiled, right-click on the assembly (which you can find in the project \bin\Debug directory) in Windows Explorer and select Properties. Figure 27-2 shows the Version information tab in Windows XP. the Description field contains the description contained in the AssemblyTitle attribute.

image from book
Figure 27-2

The assembly attributes and their corresponding names on the Version information tab are listed in the following table.

Attribute

Version Information

AssemblyTitle

Description

AssemblyDescription

Comments

AssemblyCompany

Company name

AssemblyTrademark

Legal trademarks

AssemblyVersion

Assembly version and product version

AssemblyCopyright

Copyright

AssemblyProduct

Product name

You may have noticed that the list of attributes available through the assembly Properties sheet is fewer than the list of attributes defined within the assembly — one example being the AssemblyConfiguration attribute. Microsoft has mapped some of the most common attributes onto the Properties sheet, but to get at the other attributes you'll either have to write code (shown in the upcoming "Reflection" section) or use the Ildasm or Reflector tools.

To find all attributes on a given assembly, you can use Ildasm to inspect the assembly and look for the attribute(s) defined. You were introduced to Ildasm in Chapter 26 and saw how to add it as an external tool to Visual Studio 2005. If you haven't done so, now is a good time to go back and see how to do this. Open Ildasm and select the assembly using File Open. Double-clicking the highlighted MANIFEST section will open a secondary window that contains the assembly manifest, as described in Chapter 26. Scrolling down the file a little will reveal some lines of strange-looking code — this code is the IL produced from the C# compiler:

 .assembly AttributePeek { .custom instance void  [mscorlib]System.Reflection.AssemblyCopyrightAttribute::.ctor(string)  = (01 00 00 00 00)  .custom instance void  [mscorlib]System.Reflection.AssemblyKeyNameAttribute::.ctor(string)  = (01 00 00 00 00) ... .custom instance void  [mscorlib]System.Reflection.AssemblyTitleAttribute::.ctor(string) =  ( 01 00 13 41 74 74 72 69 62 75 74 65 50 65 65 6B   // ...AttributePeek 20 62 79 20 4D 45 00 00 )                         //  by ME.. .hash algorithm 0x00008004 .ver 1:0:0:0 } 

Looking down through the file, you'll notice a number of declarations that look something like type declarations:

 [mscorlib]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 13 41 74 74 72 69 62 75 74 65 50 65 65 6B // ...AttributePeek 

The AssemblyTitle that you typed in has been persisted into the assembly manifest — if you get your hex/ASCII conversion tables out, you'll see that the set of characters after 01 00 0A are the ASCII codes for AttributePeek. Just in case you are curious, the prolog bytes 01 00 are a 2-byte ID, and 13 is the length of the string (18 characters). As mentioned earlier, this process of storing the attribute within the assembly is known as pickling, and you may come across this if you look at some of the background material on .NET available on the Web.

You may have noticed that in the code snippet from AssemblyInfo.cs, the term AssemblyTitle was used; however, in the IL code you just looked at, this is shown as AssemblyTitleAttribute. The C# compiler will look up an attribute class called AssemblyTitle first, and if it is not found it will then append the word Attribute and search again. So, whether you type the whole class name or omit the final Attribute, the same code is generated. Throughout this chapter the Attribute suffix has been dropped.

The attribute declaration persisted (pickled) into the manifest looks suspiciously like an object and its constructor. The bytes in parentheses are the parameters passed to the constructor.

Having examined the background, you can now understand the following attribute definition:

An attribute is a class that can include additional data within an assembly, concerning the assembly, or any type within that assembly. Given that an attribute is a class, and in the manifest the attribute is stored in the format shown above, let's revisit the attribute definition from earlier in the chapter:

[assembly: AssemblyTitle("AttributePeek by ME")]

The syntax is a little different from normal C#, because there are square brackets enclosing the attribute. the assembly: tag defines the scope of the attribute (which is covered later in the chapter), and the rest of the code declares the attribute. the AssemblyTitle attribute has a constructor that takes only one argument — a string value. The compiler includes this value in the assembly. This value can be accessed by the standard Windows Explorer Properties sheet, by viewing the assembly within Ildasm, or pro- grammatically by reflection — discussed in the following section.

In addition to the simple attributes dealing with assembly information, the .NET Framework defines nearly two hundred attributes used for things as diverse as debugging, design-time control behavior, serialization, and much more. You'll see some standard attributes after the "Reflection" section and then continue by seeing how to extend .NET with your own custom attributes.




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net