ASP.NET by Example
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: Because namespaces may be nested, the preceding example is functionally equivalent to 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: 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: 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: 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: 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 |