Understanding .NET (2nd Edition)
The .NET Framework class library is organized into a hierarchy of namespaces. Each namespace can contain types, such as classes and interfaces, as well as other subordinate namespaces. The root namespace is System, and every .NET Framework application will use some of the types it contains. Yet the types in several other namespaces are also likely to be commonly used by a broad swathe of developers. System is the foundation, but it's by no means the whole story.
The System Namespace
The System namespace is the ultimate parentthe rootof the .NET Framework class library. Along with its large set of subordinate namespaces, System itself contains many different types. Among the most interesting of these are the following:
Except for the base CLR types, the types in System sometimes seem to have been placed here because there was no obviously better namespace for them. Still, these types can be useful in a broad range of applications.
A Survey of System's Subordinate Namespaces
Directly below System are more than two dozen other namespaces, many of which have subnamespaces of their own. Providing even a short survey of these is a daunting task. Nonetheless, before moving on to examine the most important namespaces in this and later chapters, it's important at least to attempt a broad view. With this lofty goal in mind, this section takes an alphabetical look at most of the namespaces directly below System, providing a brief description of what each one offers. Note that this chapter (and, in fact, this book) doesn't describe every namespace in this library, and so what follows isn't a complete list.
System.CodeDom includes types that can be used to create a program directly in memory. This large set of types includes classes for generating declarations, assignments, if statements, and everything else required to construct a complete program. Even comments are supported. Once a program is created, it can be output in C#, Visual Basic (VB), or some other language. The types in this namespace can be used by any .NET developer, and they're also used by some parts of the .NET Framework itself, such as ASP.NET.
System.Collections includes types for creating and working with hash tables, arrays, queues, stacks, lists, and other generally useful data structures. These types are defined quite generally. For example, the Stack and Queue classes contain Objects, which lets them contain values of any CTS type. A subordinate namespace, System.Collections.Generic, contains essentially the same group of types, this time defined using generics rather than simple Objects. System.Collections also contains the subordinate namespace System.Collections.Specialized, which provides types for more narrowly applicable uses, such as a collection of Strings (although the types in System.Collections.Generic effectively make many of these specialized collection types redundant).
System.ComponentModel contains types for creating various kinds of .NET Frameworkbased components. Among the types it includes is the Component class, which serves as the basis for components used by Windows Forms, described in this chapter, and for many other classes in the .NET Framework class library. Components, which implement the IComponent interface defined in this namespace, exist inside instances of the Container class, also defined in this namespace. Each Container object implements two more interfaces defined in this namespace: one occurrence of the IContainer interface, along with one ISite interface for each component the container hosts. This namespace also contains types for licensing components. If you're familiar with the older COM-based mechanisms for building ActiveX controls, the types defined in this namespace should suggest those once-popular models for building components.
System.Configuration provides types such as the ConfigurationsSettings class that allow accessing configuration information for a .NET Frameworkbased application. It also contains subordinate namespaces such as System.Configuration.Assemblies for working with assembly-specific configuration information and System.Configuration.Install for building custom installers for CLR-based software.
System.Data is among the most important namespaces in the .NET Framework class library. The types in this namespace implement ADO.NET, the standard approach to accessing data for .NET Framework applications. Its subordinate namespaces include System.Data.SqlClient, which allows access to data stored in Microsoft's SQL Server, System.Data.OracleClient, which allows access to data stored in an Oracle database, System.Data.OleDb, which allows access to data sources using OLE DB providers, and System.Data.Odbc, which allows access to data sources through ODBC. ADO.NET is described in more detail in Chapter 6.
System.Diagnostics contains a large set of classes, interfaces, structures, and other types that help with debugging .NET Framework applications. For example, the Trace class allows adding assertions to code that verify key conditions, writing messages that trace the flow of execution, and performing other useful functions in released software. The Debug class, also defined in this namespace, provides similar services but is designed to be used during development rather than in a released product.
System.DirectoryServices contains types for accessing Active Directory and other directory services. Prior to .NET, the standard way to expose Windows services was through COM-based interfaces. Active Directory, for instance, can be accessed via the Active Directory Services Interface (ADSI), which is defined as a collection of COM objects. The .NET Framework has superseded COM for new interface definitions, however, and so new ways to expose services must be created using managed code. The types in the System.DirectoryServices namespace are the .NET Framework's analog to ADSI. A subordinate namespace, System.DirectoryServices.Protocols, provides an interface for accessing directories using the standard Lightweight Directory Access Protocol (LDAP).
System.Drawing provides a large set of types for using the services of the current version of Microsoft's Graphics Device Interface (GDI), known as GDI+. System.Drawing itself includes classes for working with pens, brushes, and other drawing tools, while several subordinate namespaces contain types for related uses. System.Drawing.Drawing2D, for example, contains types for vector graphics and other two-dimensional drawing functions; System.Drawing.Imaging contains types for working with metafiles and other more advanced GDI imaging; System.Drawing.Printing contains types for controlling printers; and System.Drawing.Text contains types for manipulating fonts. A System.Drawing.Design namespace is also included that provides types for customizing the user interface developers see at design time.
System.EnterpriseServices contains types for accessing the services provided by COM+, including support for distributed transactions, role-based authorization, and object pooling. Unlike most of the functions provided by the .NET Framework class library, the types contained here largely provide a wrapper around the existing COM+ software rather than reimplementing it as managed code. This important namespace is described in more detail in Chapter 7.
System.Globalization contains types for creating national calendars, converting to national code pages, formatting dates and times, and other aspects of building software that supports multiple cultures. Globalized software is important, and so many of the .NET Framework's basic functions are automatically culture-aware. For example, conversion to a currency value can automatically examine the caller's culture setting to format the currency appropriately.
System.IO provides a large set of types for reading and writing files, directories, and in-memory streams. While access to a database management system (DBMS) is often the main route applications take to data, the ability to work with files is still important. System.IO also contains a few subordinate namespaces that provide related services. System.IO.Compression, for example, allows reading and writing compressed data using the GZIP standard. The basics of System.IO are described later in this chapter.
System.Management provides types for accessing Windows Management Instrumentation (WMI) data from managed code. WMI is Microsoft's implementation of the Web-Based Enterprise Management (WBEM) initiative supported by many different vendors. A number of the types in this namespace provide support for the WMI Query Language (WQL), a dialect of SQL focused on accessing WMI-related information.
System.Media includes classes for working with sounds. For example, this simple namespace provides a SoundPlayer class with methods such as Play and Stop, allowing a CLR-based application to start and stop playing of a sound file.
System.Messaging contains types for accessing Microsoft Message Queuing (MSMQ), Microsoft's solution for Windows-to-Windows message queuing. MSMQ has several other application programming interfaces (APIs), including a COM-based API and an API defined as a set of C function calls. System.Messaging's types define the MSMQ API that managed code should use.
System.Net provides types for accessing several common protocols, including HTTP, FTP, and the Domain Name System (DNS). It also contains the abstract classes WebRequest and WebResponse, which allow building applications that are unaware of what protocol is being used to communicate. These applications can simply make requests to and get responses from specified URLs and let the underlying software worry about the details. Version 2.0 of the .NET Framework adds the HttpWebListener class, which allows a developer on newer versions of Windows to create simple Web servers. System.Net also contains a number of subordinate namespaces. System.Net.Sockets, for example, provides a managed implementation of the traditional sockets interface to TCP and UDPit's WinSock for the .NET generationwhile System.Net.Mail allows sending mail to an SMTP server.
System.Reflection contains a large set of types for examining an assembly's metadata. A subordinate namespace, System.Reflection.Emit, contains types that can be used to create other types dynamically. Reflection is described in more detail later in this chapter.
System.Resources provides types that allow managed code to work effectively with resources. Resources are parts of an application that can be separated from the source code, such as message strings, icons, and bitmaps. A primary use of this namespace's types is to allow an application to display different resources easily when used in different cultures. This makes the contents of this namespace especially relevant for internationalized software that must work in many different countries and languages.
System.Runtime is a parent namespace that contains several important subordinate namespaces. One of these, System.Runtime.InteropServices, is one of the .NET Framework class library's most important namespaces. It contains types that help in interoperating with non-CLR-based software, such as COM classes, and is described in more detail later in this chapter. System.Runtime.Remoting is another important namespace, as the types it contains allow accessing managed objects in other processes and other machines. This technology, known as .NET Remoting, is described in more detail in Chapter 7. System.Runtime.Serialization contains types for serializing a managed object's state. Serializing a managed object means copying its state (although not its code) into either memory or some more permanent medium such as a file. The ability to work with an object's state in this way is a fundamental feature of the .NET Frameworkit's used by .NET Remoting, for exampleand this namespace is also described in more detail later in this chapter.
System.Security contains classes, interfaces, enumerations, and subordinate namespaces that provide various security-related functions. The namespace directly contains several fundamental classes, such as the SecurityManager class, which is the primary access point for working with the security system. The subordinate namespace System.Security.Cryptography contains types for using secret and public key cryptography services. Those types provide access to the Windows Cryptographic Service Providers (CSP) that actually implement algorithms such as DES, RC2, and RSA. System.Security.Cryptography itself also contains a few subordinate namespaces. One of them, System.Security.Cryptography.X509Certificates, contains classes for creating and using X.509 version 3 public key certificates for use with Microsoft's Authenticode technology, while another, System.Security.Cryptography.Xml, implements the World Wide Web Consortium (W3C) standard for digitally signing data described using XML.
System.Security also contains a few other subordinate namespaces. They include System.Security.Permissions, which defines types such as a class representing each of the possible permissions for code access security, and System.Security.Policy, which defines classes such as Site, URL, Publisher, Zone, and others used in defining security policy. How these concepts are used by the CLR was described briefly in Chapter 2's section on code access security. Another child namespace of System.Security, System.Security.Principal, contains types for working with security principals. These classes are used by the CLR in implementing the role-based security described in Chapter 2. System.ServiceProcess contains types for building .NET Framework applications that run as long-lived processes called Windows services. This kind of application was previously known as an NT service and is also sometimes referred to as a daemon.
System.Text contains a group of classes for working with text. For example, this namespace's UTF8Encoding class can convert Unicode characters from their default encoding into UTF-8 and vice versa. UTF stands for Unicode Transformation Format, and UTF-8 is compatible with the familiar ASCII character representation (although it allows representing multibyte non-ASCII characters, too). System.Text also contains the namespace System.Text.RegularExpressions. The types in this namespace allow access to a generic regular expression engine that can be used from any CLR-based language.
System.Threading is another of the .NET Framework's more important namespaces. The types it contains provide a standard way for developers working in any .NET language to build multithreaded applications. In the pre-.NET era, VB, C++, and other languages all had their own unique approach to threading, with COM's apartments serving as a (complicated) cross-language solution. With the .NET Framework, all CLR-based languages can use the contents of System.Threading to work with threads in a consistent way. Perhaps the most important type in this namespace is the Thread class, which provides methods to start a thread, stop one, cause it to wait for another thread, and more. The namespace also contains classes for using fundamental synchronization constructs, such as mutexes and monitors, and for working with a thread pool. System.Timers contains types for specifying and handling recurring events. The most important class in this namespace is Timer, which allows a developer to specify an interval at which an Elapsed event, defined as part of the Timer class, will be raised in his application. The application can then catch this event and perform some function. For example, an application may wish to check for new mail once every ten minutes. Using this mechanism, the developer could cause the Elapsed event to take place every ten minutes, then put mail-checking code in the handler for this event.
System.Transactions, a new namespace in version 2.0 of the .NET Framework, contains types that let applications use transactions. Prior to this 2005 release, applications could use ADO.NET for handling database transactions or Enterprise Services for working with more complex, multisystem transactions. With System.Transactions, Microsoft rethought how applications should use transactions on Windows. This important new technology is described in more detail later in this chapter.
System.Web is, after System itself, perhaps the most important namespace in the .NET Framework class library. Comprising many types and many subordinate namespaces, the software it contains implements ASP.NET. The two most important children of System.Web are System.Web.UI, which contains types for building browser-accessible applications, and System. Web.Services, which contains types for creating applications that expose Web services. ASP.NET's support for creating browser-based applications is described in Chapter 5, while Chapter 7 looks at the support it provides for Web services.
System.Windows.Forms contains types used to construct local Windows Graphical User Interfaces (GUI). Local GUIs are somewhat less important for application developers than they once werea browser interface is now more commonbut they're nonetheless an important topic. Accordingly, this namespace is described in more detail later in this chapter.
System.XML contains types useful for working with XML documents. The .NET Framework is shot through with support for XML, and the contents of this namespace provide a significant amount of technology for developers working with XML-defined data. This namespace is also described in more detail later in this chapter.
Code provided by a standard library isn't useful unless you know it's there. Accordingly, while memorizing the entire .NET Framework class library isn't necessary, any developer writing CLR-based code should have a broad grasp of what the library makes available. One way to think about what's available is to group the library's services into a few broad categories:
The rest of this book is structured in this way. The remainder of this chapter describes some of the library's most important general services, while the chapters that follow examine the other three categories. |
Категории