Attributes

As explained in Chapter 1, assemblies are self-describing because they contain their own metadata, avoiding the need for registries and other catalogs. Attributes introduce a way to add custom metadata to an assembly. They add declarative logic to code, making it easier for tools and special libraries to make decisions about code just by inspecting the attributes.

There are many more attributes in the .NET Framework libraries than can be covered in this section. An in-depth discussion about each attribute is beyond the scope of this book. Therefore, the attributes presented are simply representative of the concepts you need to know and understand to use attributes. For more information, search for "attributes" in the C#Builder Help Index.

Attributes Without Parameters

The simplest attributes to use don't have any parameters. They simply declare the capabilities of a type. A common attribute fitting in this category is the Serializable attribute. Serialization is the process of moving a type from one place to another, such as across the network. This has important implications in distributed computing, where a type must be serializable to be sent to a Remoting object or Web Service somewhere else on a network. Listing 4.5 features the Serializable attribute as an example of how to use an attribute without parameters.

Listing 4.5 An Attribute Without Parameters (AttributeNoParams.cs)
 using System; [Serializable] public class MyClass {    // some implementation } class AttributeNoParams {    static void Main()    {    } } 

The Serializable attribute is simply the identifier surrounded by square brackets. Networking types examine this attribute to determine whether the type it decorates can be moved across the network.

A common point of confusion occurs when the documentation and many examples vary in their presentation of attribute identifiers. Each attribute officially ends in the word "Attribute", as in SerializableAttribute. C# allows shortcuts where the Attribute suffix may be omitted. However, both identifiers have the same meaning and both are correct.

Attributes with Positional Parameters

Many attributes have what are called positional parameters. They are declared in a specific order in the attribute, just like a method call. Because an attribute is just another class, it has constructors, which can be overloaded. The positional parameters correspond to constructor parameters for the attribute class being used.

A simple, but useful, attribute with a positional parameter is the CLSCompliant attribute. Recall the discussion in Chapter 1 about the Common Language Specification (CLS). The CLSCompliant attribute helps determine whether C# code should adhere to the CLS Specification. Listing 4.6 uses the CLSCompliant attribute to demonstrate an attribute with a positional parameter.

Listing 4.6 An Attribute with a Positional Parameter (AttributePositional.cs)
 using System; [assembly: CLSCompliant(true)] public class MyClass {    // some implementation } class AttributePositional {    static void Main()    {    } } 

The CLSCompliant attribute in Listing 4.6 has a single parameter, true, indicating that this assembly will be checked for CLS compliance during compilation. The syntax assembly: declares that the target of this attribute is for an assembly. A target helps scope an attribute to the element it decorates, which could be assembly, class, method, return, or several others as defined in the .NET Framework SDK documentation for the AttributeUsage attribute definition.

Attributes with Named Parameters

Named attribute parameters correspond to public fields and properties of the attribute class. They are called "named" because their name is included in the parameter when their attribute is used. Named parameters may be presented in any order, but all of them must follow positional parameters.

An important attribute with named parameters is the DllImport attribute. It is what implements P/Invoke, enabling managed code to call native Win32 DLLs. Listing 4.7 uses the DllImport attribute to demonstrate using named parameters in attributes.

Listing 4.7 Demonstration of an Attribute Containing a Named Parameter (AttributeNamed.cs)
 using System; using System.Runtime.InteropServices; class AttributeNamed {    [DllImport("winmm.dll", SetLastError=true)]    public static extern long PlaySound(       string fileName, long hModule, long dwFlags);    static void Main()    {       PlaySound(@"c:\Windows\Media\ding.wav", 0, 0);    } } 

The DllImport attribute in Listing 4.7 has a positional parameter, "winmm.dll", and a named attribute, SetLastError=true.



C# Builder KickStart
C# Builder KickStart
ISBN: 672325896
EAN: N/A
Year: 2003
Pages: 165

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