Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Manning - Windows Forms Programming With CSharp.pdf
Скачиваний:
72
Добавлен:
24.05.2014
Размер:
14.98 Mб
Скачать

The remainder of this appendix discusses specific namespaces under the System umbrella. Each section discusses a separate namespace, with the sections arranged in alphabetical order.

For additional information on these and other namespaces in .NET, see the resources listed in Appendix D and in the bibliography. For some sample applications along with a discussion of many of these namespaces, see the book Microsoft .NET for Programmers by Fergal Grimes, available from Manning Publications.

B.1 SYSTEM.COLLECTIONS

The System.Collections namespace defines various types required to manipulate collections of objects, including lists, queues, stacks, hash tables, and dictionaries. An exception is the Array class, which is part of the System namespace, since this class provides core functionality defined by the C# language.

Members of this namespace are discussed throughout the book, and in particular in chapter 5, where the PhotoAlbum class is built as a collection of Photograph objects.

B.2 SYSTEM.COMPONENTMODEL

This namespace defines various types that define the runtime and design-time behavior of components and controls. In particular, this class defines the Component and Container classes and their corresponding interfaces.

The Component class is introduced in chapter 3 as the base class for much of the functionality in the Windows Forms namespace. Members of this namespace are also critical for data binding support, which is discussed in chapter 17.

B.3 SYSTEM.DATA

The System.Data namespace defines classes and other types that constitute the ADO.NET architecture. This architecture enables the manipulation and management of data from multiple data sources, including both local and remote databases and connected or disconnected interaction.

Although this namespace is not discussed in detail in the book, chapter 17 provides some details on using databases with the data binding interface supported by Windows Forms, and in particular with the Windows.Forms.DataGrid control.

See the bibliography for references to additional information on this namespace, and in particular the book ADO.NET Programming by Arlen Feldman, available from Manning Publications.

B.4 SYSTEM.DRAWING

This namespace defines basic functionality in the GDI, or graphical device interface, architecture. This includes the Graphics class for drawing to a device, as well as the Pen class for drawing lines and curves and the Brush class used to fill the interiors of

SYSTEM.DRAWING

675

Data-

shapes. It also includes the Point, Size, Rectangle and other structures used for positioning and sizing Windows Forms controls within a container.

The System.Drawing.Design namespace provides design-time support for user interface logic and drawing. The UITypeEditor class in this namespace can be used to provide a graphical editor for a type, including types used with Windows Forms controls.

An overview of the System.Drawing namespace is provided in .NET Table 4.6 on page 124. Members of this namespace are used in chapter 4 to draw a rectangle into an owner-drawn StatusBar control; in chapter 7 and elsewhere to paint an image; and in chapter 10 to draw both an image and text into an owner-drawn ListBox control.

B.5 SYSTEM.GLOBALIZATION

The System.Globalization namespace defines locale-related information such as the formatting of dates, times, currency, and numbers.

A number of Windows Forms controls include some sort of formatting property that can be used to specify formatting information. Chapter 11 discusses the TimeFormatInfo class defined in this namespace, and the MonthCalendar control that relies on the calendar information maintained by classes within this namespace. Chapter 17 also introduces the Format event in the Windows.Forms.Binding class that can be used to specify how bound data should be formatted for a particular data binding.

B.6 SYSTEM.IO

This namespace defines types for performing synchronous and asynchronous reading and writing of data streams and files. It also defines types for interacting with the file system, such as the Directory, File, and Path classes.

The FileStream, StreamReader, and StreamWriter classes are introduced in chapter 6 in order to read and write album files from the MyPhotos application. Members for interacting with the file system are discussed here as well.

For detailed information on this namespace, consult the references listed in Appendix D and the bibliography.

B.7 SYSTEM.NET

The System.Net namespace defines types for common Internet protocols such as HTTP and local file management, including the abstract WebRequest and WebResponse classes. The related System.Net.Sockets namespace defines a managed implementation of the Windows Sockets interface.

These interfaces can be very useful in Windows Forms applications for interacting with remote servers and services, and for building custom communication interfaces between one or more applications. In Windows Forms applications, it is common to create a specific thread responsible for external communication of this kind, rather

676

APPENDIX B .NET NAMESPACES

Prop-

than performing such communication as part of a user interface thread. See the discussion on the System.Threading namespace later in this appendix for more information on threading.

Since the programs in this book are designed to be standalone applications, neither of these interfaces is discussed in the book.

B.8 SYSTEM.REFLECTION

This namespace defines a managed view of loaded types and their members, including classes and their methods, properties, and events. It supports the ability to dynamically create new types and invoke existing types and their members. For example, the classes in this namespace can be used to query the classes in an assembly and invoke specific properties and methods within that assembly.

Windows Forms controls use this namespace internally to query and interact with various types of objects. A brief exercise at the end of chapter 10 discusses how the ListBox control uses reflection to determine the value of the DisplayMember setting in this control, and illustrates how to invoke a property by name using the ertyInfo class. Reflection is also briefly discussed in chapter 17.

B.9 SYSTEM.RESOURCES

The System.Resources namespace defines types that permit programs to create, store, and manage resources used by an application. Resources can be stored in a loaded assembly or in a satellite assembly that is external to the application. In particular, this namespace is used to manage culture-specific resources for an application, and is used for localization of applications.

Localization is the process of building an interface that can be used in multiple cultures and languages. Typically, it involves placing strings, images, and other cul- ture-specific resources into a resource file, and loading such resources dynamically at runtime. This resource file can then be translated into another language or based on another culture to generate alternate resource files. These alternate resource files can then be used with the same program assembly to execute the program in the corresponding language or culture.

For example, while the applications in this book were written for a U.S. English user, we might want to support users that understand Canadian French, or Mexican Spanish. Placing our original strings and other constructs in a separate resource file would allow us to do just this.

If you are interested in writing an application targeted at multiple cultures, it is worth your time to understand this process before you begin. It can be quite difficult to localize an existing program, rather than building in such support from the start.

While the book does not discuss localization in particular, a brief discussion of resources can be found while discussing the storage of images in chapter 12 and again

SYSTEM.RESOURCES

677

Applica-

in chapter 13. The .NET documentation provides some sample programs that illustrate localization, as do many of the resources provided later in the book.

B.10 SYSTEM.SECURITY

This namespace defines the common language runtime security system, including security permissions for code and assemblies. The SecurityManager class in this namespace is the main access point for classes interacting with the security system.

This namespace is beyond the scope of the book, and aside from a brief mention in chapter 9, it is not really discussed. See the resources listed in appendix D and the bibliography for more information, and in particular look at .NET Security by Tom Cabanski, which is available from Manning Publications.

B.11 SYSTEM.THREADING

The System.Threading namespace defines the types that enable multithreaded programming, including the Thread class and synchronization primitives such as the Monitor and Mutex classes. A thread is a sequence of execution corresponding to a defined set of computer instructions. All C# programs in .NET begin with a Main method, running in what is called the main thread. This main thread may create, or spawn, additional threads as required. Each thread performs a defined task or set of tasks. At a basic level, multiple threads simply permit a program to do multiple things at once.

Generally speaking, threads are either interface threads or worker threads. An interface thread is a thread that interacts with the user in some fashion. The main thread in a Windows Forms program is typically an interface thread, and the tion.Run method introduced in chapter 1 is used to start a message loop on this thread which receives operating system messages and converts them into .NET events that invoke event handlers registered with the program.

A worker thread is a thread that performs some kind of analysis or other work on behalf of a program, and typically is hidden from a user. For example, a worker thread might receive stock price information from a remote server that a user interface thread displays in a ListView control.

Threads are created using the Thread class, with a ThreadStart delegate specifying the method or other program code that should be executed within the thread. The trick with multithreaded programming isn’t the ability to have multiple threads; it is the synchronization, or co-existence, of these threads that causes difficulties. For this reason, synchronization constructs such as locking have evolved to control the interaction between multiple threads, and to make sure that different threads do not access the same portion of memory, databases, or other shared data at the same time.

As we focused on the Windows Forms namespace in this book, our examples did not include multiple threads of control. For a detailed discussion of how threads are used in .NET applications, including Windows-based programs, see .NET Multithreading by Alan Dennis, available from Manning Publications.

678

APPENDIX B .NET NAMESPACES