Component-Based Features

I l @ ve RuBoard

Up to this point, we've focused on programs and even program fragments in introducing the concepts of the language. However, C# has several features that enable building component-based applications: namespaces, assemblies, and attributes.

Namespaces and Assemblies

Namespaces provide a logical organizational system, both internally within the program and externally to other programs. Consider the following example:

  namespace Que.ByExample {     class Book     { ...} 

Because namespaces may be nested, the preceding example is functionally equivalent to

  namespace Que {     namespace ByExample     {         class Book         { ...}     } } 

In either case, we can reference the Book class via its fully qualified namespace:

 Que.ByExample.Book 

We can avoid using the fully qualified namespace by using the using directive as we have seen with the System namespace in prior examples:

  using Que.ByExample; 

Assemblies are used for physical packaging and deployment by acting as containers for types. Assemblies take two forms, .exe files, which include a main entry point, and .dll libraries, which do not have a main entry point. The C# compiler produces .exe applications by default. The compiler produces libraries by specifying the target switch as follows :

 csc /target:library filename.cs 

Attributes

C# supports attributes whereby programmers can specify declarative information for program entities and retrieve this information at runtime via reflection. In fact, the .NET Framework provides a number of special case attribute types already. Take the following example for instance:

  [ WebMethod(Description="Returns the stock price", EnableSession=true)] public decimal GetCurrentStockPrice() { ...} 

In this fragment from a stock lookup Web service, we have marked the GetCurrentStockPrice method with the WebMethod attribute, which denotes it as callable via Web services. Just as the .NET Framework team developed the WebMethod attribute, we can derive our own custom attributes from the System.Attribute class:

  using System; [AttributeUsage(AttributeTargets.All)] public class HelpDocAttribute : Attribute {     public string Keywords;     private string url;     private string topic;     public HelpDocAttribute(string url, string topic)     {         this.url = url;         this.topic = topic;     }     public string Url     {         get         {             return url;         }     }     public string Topic     {         get         {             return topic;         }     } } 

In creating our custom attribute, we first have to denote on which targets our attribute can be used. We do this, ironically, by using another attribute on our class declaration, the AttributeUsage class. Note that we specify our attribute to be available on any elements by passing the enumerated value AttributeTargets.All to the AttributeUsage constructor. Please consult the .NET Framework documentation for available values of the AttributeTargets enumeration.

After designating which targets our attribute will be used on, we next set up our positional and named parameters. For custom attributes, positional parameters are those corresponding to the arguments for the public constructors of the attribute. Named parameters are those defined by public read/write properties of the class. Note that in our example we have created two positional parameters, Url and Topic , and one named parameter, Keywords .

Once declared, our custom attribute may be employed in this way:

  [HelpDocAttribute("http://help.xyzcompany.com/docs/SportsTicker.aspx",     "SportsTicker Class")] public class SportsTicker {     [HelpDocAttribute("http://help.xyzcompany.com/docs/SportsTicker.aspx",          "SportsTicker Constructor", Keywords="SportsTicker")]     public SportsTicker(){ ...} } 

Notice that for the SportsTicker class itself, we pass the positional parameters of Url and Topic , and for the public constructor, we add a third parameter for a keyword search.

I l @ ve RuBoard


Asp. Net. By Example
ASP.NET by Example
ISBN: 0789725622
EAN: 2147483647
Year: 2001
Pages: 154

Similar book on Amazon

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