Tải bản đầy đủ (.pdf) (578 trang)

2002 mh advanced c sharp programming

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.06 MB, 578 trang )


Advanced
C# Programming


About the Author
Paul Kimmel founded Software Conceptions, Inc., in Okemos, Michigan in 1990. Paul
specializes in object-oriented architecture and software design. He is the frameworks columnist
for Windows Developer Magazine, a bimonthly columnist for codeguru.com, and a contributing
columnist to InformIT. Paul has written several books on object-oriented programming. He is
currently the lead architect for a C# and ASP.NET enterprise application for managing the
corrections division of the sherrif’s office in Oregon, and he has successfully implemented
dozens of corporate and commercial applications in the telecom, insurance, and financial
industries and for law enforcement and government agencies, with customers throughout the
United States, Canada, and Puerto Rico.


Advanced
C# Programming

Paul Kimmel

McGraw-Hill/Osborne
New York Chicago San Francisco
Lisbon London Madrid Mexico City
Milan New Delhi San Juan Seoul
Singapore Sydney Toronto


Copyright © 2002 by The McGraw-HIll Companies, Inc. All rights reserved. Manufactured in the United States of
America. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be


reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior
written permission of the publisher.
0-07-222828-8
The material in this eBook also appears in the print version of this title: 0-07-222417-7

All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark
owner, with no intention of infringement of the trademark. Where such designations appear in this book, they
have been printed with initial caps.
McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for
use in corporate training programs. For more information, please contact George Hoare, Special Sales, at
or (212) 904-4069.

TERMS OF USE
This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all
rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act
of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse
engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish
or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your
own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work
may be terminated if you fail to comply with these terms.
THE WORK IS PROVIDED “AS IS”. McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES
OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE
OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED
THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not
warrant or guarantee that the functions contained in the work will meet your requirements or that its operation
will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for
any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom.
McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been
advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise.

DOI: 10.1036/0072228288


Want to learn more?
,

We hope you enjoy this McGraw-Hill eBook! If you d like
more information about this book, its author, or related books
and websites, please click here.


For Trevor, Douglas, and Noah,
I love you for the good-natured, loving boys that you are
and the fine men I know you will be.
Love, Fat Daddy


This page intentionally left blank.


For more information about this title, click here.

Contents at a Glance
Part I

Windows Applications

Chapter 1

Language Foundations . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


3

Chapter 2

Assembly Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

35

Chapter 3

Video Kiosk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

63

Chapter 4

Terrarium . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

Chapter 5

Building Database Applications with ADO.NET . . . . . . . . . . . . . . . 133

Part II

Tools and Components

Chapter 6

Customizing Visual Studio .NET . . . . . . . . . . . . . . . . . . . . . . . . 169


Chapter 7

Event Logger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

Chapter 8

Creating UserControls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

Chapter 9

Special Effects Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261

Chapter 10

Creating Custom Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . 301

Chapter 11

Practical Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319

Part III

Web Applications—IBUYSPY Portal

Chapter 12

Implementing Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . 371

Chapter 13


IBuySpy and Dynamic User Interfaces in ASP.NET . . . . . . . . . . . . . 399

Chapter 14

Creating Custom Web Controls . . . . . . . . . . . . . . . . . . . . . . . . 435

Copyright 2002 by The McGraw-Hill Companies, Inc. Click Here for Terms of Use.

vii


viii

Advanced C# Programming

Chapter 15

Output Caching and Persisting State Information . . . . . . . . . . . . . . 469

Chapter 16

Security and Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . 505

Index

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529


For more information about this title, click here.


Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Part I
Chapter 1

Chapter 2

xxi
xxiii

Windows Applications
Language Foundations . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

Object-Oriented Basics . . . . . . . . . . . . . . . . .
Defining Classes . . . . . . . . . . . . . . . .
Creating Instances of Classes . . . . . . . . . .
Defining Interfaces . . . . . . . . . . . . . . .
Implementing Interfaces . . . . . . . . . . . .
Inheritance . . . . . . . . . . . . . . . . . . .
Encapsulation and Aggregation . . . . . . . . .
Polymorphism . . . . . . . . . . . . . . . . .
Using Modifiers . . . . . . . . . . . . . . . . .
Operator Overloading . . . . . . . . . . . . . . . . .
Coding Operator Methods . . . . . . . . . . . .
Overloading Operator Guidelines and Limitations

Attributes . . . . . . . . . . . . . . . . . . . . . . .
Reflection . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

4
5
22
23
24
25
28
29
30
31

31
32
33
34
34

Assembly Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

35

Demonstrated Topics . . . . . . . . . . . . .
Code Listing for the Assembly Manager . . . .
using Statements . . . . . . . . . . .
Defining Your Namespace Name . . .
Defining Classes . . . . . . . . . . .
Walking the Assembly Using Reflection
Broadcasting Operations . . . . . . .

36
36
39
40
41
43
45

.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.

Copyright 2002 by The McGraw-Hill Companies, Inc. Click Here for Terms of Use.

ix


x

Advanced C# Programming

Secondary Topics . . . . . . . . . . . . . .
Value Types Versus Reference Types
Windows Forms Controls . . . . . .
Basic Printing . . . . . . . . . . . .
Embedded Resource File Management
Multithreading . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . .

Chapter 3

Chapter 4

.
.
.
.

.
.

.
.
.
. .
. .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

49
49
52
57

57
58
62

Video Kiosk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

63

Demonstrated Topics . . . . . . . . . . . . . . . . . . . . . .
What Is GDI+? . . . . . . . . . . . . . . . . . . . . . . . . .
GDI+ Namespaces . . . . . . . . . . . . . . . . . . .
Understanding the GDI+ Programming Model . . . . . .
Examining the PlayControl . . . . . . . . . . . . . . . . . . .
Implementing the PlayControl . . . . . . . . . . . . .
Creating the Graphical User Interface for the PlayControl
Implementing the Tracker Control . . . . . . . . . . . .
Secondary Topics . . . . . . . . . . . . . . . . . . . . . . . .
Implementing the Elapsed Time Clock . . . . . . . . . .
Using the ToolTip Control . . . . . . . . . . . . . . . .
Adding Controls to the Toolbox . . . . . . . . . . . . .
Catching and Handling Specific Exceptions . . . . . . .
Adding TODO Items to the Task List . . . . . . . . . . .
Using the Process Class . . . . . . . . . . . . . . . . .
COM Interop . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . .

64
64
65
65

67
67
71
87
93
93
94
95
95
96
99
99
100

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

Terrarium . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Demonstrated Topics . . . . . . . . . . . . . . .
Downloading, Installing, and Configuring Terrarium
Configuring the Terrarium Client . . . . .
Running the Terrarium Client . . . . . . .
Configuring the Terrarium Server . . . . .
Playing Terrarium . . . . . . . . . . . . . . . .
Environment Reporting Every Six Minutes .
Critter Timing . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

102
103
104
105
106
107
107
111



Contents

Teleporter . . . . . . . . . . . . . . . . . . . .
Smart Client Updates . . . . . . . . . . . . . . .
Peer-to-Peer Computing . . . . . . . . . . . . .
Support for Multiple Programming Languages . .
Code Access Security . . . . . . . . . . . . . . .
Reviewing the Terrarium Framework . . . . . . . . . . .
Creating Plants and Critters . . . . . . . . . . . . . . . .
Creating a Plant . . . . . . . . . . . . . . . . .
Creating Critters . . . . . . . . . . . . . . . . .
Introducing Plants and Critters to the Terrarium . . . . . .
Introducing Code Access Security . . . . . . . . .
Secondary Topics . . . . . . . . . . . . . . . . . . . . .
Serializing Objects . . . . . . . . . . . . . . . . . . . .
Serializing Organisms Using MemoryStream Objects
Writing to a FileStream . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . .

Chapter 5

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

112
112
115
115
115
116
116
117
118
128
129
129
129
130
131
132

Building Database Applications with ADO.NET . . . . . . . . . . . . . . . 133
Demonstrated Topics . . . . . . . . . . . . . . .

A Quick Review of ADO.NET Namespaces . . . . .
Connecting to DataSources . . . . . . . . . . . .
Connecting to an OLE DB Data Store . . .
Connecting to an MS SQL Server Data Store
Using ADO.NET Interfaces to Declare Types
Understanding the Role of the Adapter . . . . . .
Initializing an Adapter . . . . . . . . . .
Invoking the Adapter Fill Method . . . . .
Invoking the Adapter FillSchema Method .
Updating Changes to Data . . . . . . . .
Working with the DataSet . . . . . . . . . . . .
Adding DataTable Objects to a DataSet . .
Creating Master-Detail Relationships . . .
Creating Data Column Mappings . . . . .
Using the DataTable . . . . . . . . . . . . . . .
Creating a DataTable Object . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

134
135
135
136
139
139
143
144
146
148
148
150
150
151
153
155
156

xi


xii

Advanced C# Programming


Creating a Primary Key, Auto-Incremented Column
Walking the Properties of the PropertyInfo Class .
Using the DataView . . . . . . . . . . . . . . . . . . .
Using the DataReader for Read-Only Data . . . . . . . .
Displaying Information in the DataGrid . . . . . . . . . .
Using the Command Object . . . . . . . . . . . . . . . .
Generating SQL with the CommandBuilder . . . . . . . .
Secondary Topics . . . . . . . . . . . . . . . . . . . . .
Binding a DataSet to a DataGrid . . . . . . . . . . . . .
Returning a DataSet from a Web Service . . . . . . . . .
Implementing a TraceListener . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . .

Part II
Chapter 6

.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

156
157
158

159
159
161
162
162
163
163
164
166

Tools and Components
Customizing Visual Studio .NET . . . . . . . . . . . . . . . . . . . . . . . . 169
Demonstrated Topics . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Custom Wizard . . . . . . . . . . . . . . . . . . . . . .
Creating the Wizard Project . . . . . . . . . . . . . . . . . .
Implementing the IDTWizard Interface . . . . . . . . . . . .
Passing Arguments by Reference . . . . . . . . . . . . . . .
Testing the Wizard . . . . . . . . . . . . . . . . . . . . . .
Creating a Project Template for Visual Studio .NET Wizards . . . . . .
Copying an Existing Project Template . . . . . . . . . . . . .
Adding Files for the Wizard Library Template . . . . . . . . .
Modifying the default.js Script . . . . . . . . . . . . . . . .
Modifying the common.js Script . . . . . . . . . . . . . . . .
Creating the Wizard Launch File . . . . . . . . . . . . . . .
Creating the VSDir File . . . . . . . . . . . . . . . . . . . .
Extending Visual Studio .NET with Wizards . . . . . . . . . . . . . .
Writing a Macro . . . . . . . . . . . . . . . . . . . . . . .
A Brief Introduction to the Common Environment Object Model
Writing Code with Macros . . . . . . . . . . . . . . . . . . .
Creating the Code Generator . . . . . . . . . . . . . . . . .

Implementing the Wizard User Interface . . . . . . . . . . .
Implementing the Wizard . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

170
170
172
172
173
173

175
176
177
178
180
181
182
184
185
186
187
188
191
194


Contents

Completing the Sample Wizard Launch File . . .
Registering Wizards . . . . . . . . . . . . . .
Testing the Wizard with a Macro . . . . . . . .
Running the Wizard from the Command Window
Secondary Topics . . . . . . . . . . . . . . . . . . . .
Returning to Jscript .NET . . . . . . . . . . . . . . . .
Using the regasm Utility . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . .

Chapter 7

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

196
196
197
197
198
198
199
200

Event Logger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Demonstrated Topics . . . . . . . . . . . . . . .
Exploring System.Diagnostics Namespace . . . . .
Using the EventLog Component . . . . . . . . . .
Writing to the EventLog . . . . . . . . . .
Handling EntryWritten Events . . . . . . .
Logging Events to a Remote Machine . . .
Tracing as a Debugging Strategy . . . . . . . . .
Tracing to the Event Log . . . . . . . . .
Dumping the Stack . . . . . . . . . . . . . . . .

Using the StackTrace as a Diagnostic Tool .
Using the StackFrame as a Diagnostic Tool
Managing Debug Code Automatically . . . . . . .
Conditional Compilation . . . . . . . . .
Using Switches . . . . . . . . . . . . . .
Implementing the Logger . . . . . . . . . . . . .
Creating an Event Source . . . . . . . . .
Determining if a Source Exists . . . . . .
Deleting an Event Source . . . . . . . . .
Deleting a Custom Log . . . . . . . . . .
Adding a Trace Listener . . . . . . . . . .
Secondary Topics . . . . . . . . . . . . . . . . .
Creating Dialog Boxes . . . . . . . . . . . . . .
Dialogs Return a DialogResult . . . . . .
Private Constructors for Dialogs . . . . . .
Passing Arguments by Reference in C# . .
FileVersion Information . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

202
203
203
204
205
206
207
208
209
209
211
211
212
215
216

219
220
220
220
221
221
221
222
222
223
224

xiii


xiv

Advanced C# Programming

Using the Debug Class . . . . . . . . . . . . . . . . .
Writing Debug Information to the Output Window
Assert Everything . . . . . . . . . . . . . . . .
A Brief History of Debugging Tools . . . . . . .
Enabling and Disabling the Debug Class . . . . .
Measuring Performance . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . .

Chapter 8

.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

225
225
225
226
227
228
230


Creating UserControls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Demonstrated Topics . . . . . . . . . . . . . . . . .
Understanding UserControls . . . . . . . . . . . . .
What Is a UserControl? . . . . . . . . . . . .
GUI Is King . . . . . . . . . . . . . . . . . .
Creating a Control Library . . . . . . . . . .
Creating a ButtonCluster Control . . . . . . . . . . .
Designing the UserControl Visual Interface . .
Surfacing Constituent Events . . . . . . . . .
Creating a PickList Control . . . . . . . . . . . . . .
Adding and Removing Elements from ListBoxes
BeginUpdate and EndUpdate . . . . . . . . .
Implementing an AboutBox Control . . . . . . . . . .
Drawing and Coding the AboutBox . . . . . .
Surfacing Constituent Properties . . . . . . .
Defining a Data Bound UserControl . . . . . . . . . .
Implementing the UserControl . . . . . . . .
Implementing the ContactInformation Class . .
Binding and Navigating . . . . . . . . . . . .
Custom Painting in UserControls . . . . . . . . . . .
Transparent UserControl Background . . . . . . . . .
Extending UserControls Through Inheritance . . . . .
Secondary Topics . . . . . . . . . . . . . . . . . . .
Loading ListBoxes . . . . . . . . . . . . . . . . . .
Using BeginUpdate and EndUpdate . . . . . .
Using the ThreadPool to Load a ListBox . . . .
Dynamically Positioning and Sizing Controls . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . .


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

232
232
233
233
234
234
235
239

244
244
246
246
247
247
248
248
250
252
253
254
254
255
255
256
256
258
259


Contents

Chapter 9

Special Effects Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Demonstrated Topics . . . . . . . . . . . . . . . . . . . .
Rapid Control Prototyping . . . . . . . . . . . . . . . . .
Implement a PaintEventHandler for Custom Painting
Pass an Instance of the Control to Methods . . . . .

Examining the Problem . . . . . . . . . . . . . . .
Creating a Class Library . . . . . . . . . . . . . . . . . .
Abstract Generic Behavior . . . . . . . . . . . . .
Define a New Control . . . . . . . . . . . . . . . .
Adding the Finishing Touch . . . . . . . . . . . . .
Testing Your Component . . . . . . . . . . . . . . . . . .
Implementing a Test Function . . . . . . . . . . . .
Implementing Trap Behavior . . . . . . . . . . . .
Adding the Component to the Toolbox . . . . . . . . . . .
Creating a Merge Module . . . . . . . . . . . . . . . . . .
Secondary Topics . . . . . . . . . . . . . . . . . . . . . .
Creating a Type Converter . . . . . . . . . . . . . . . . .
Implementing IConvertible . . . . . . . . . . . . .
Implementing a TypeConverter . . . . . . . . . . .
Implementing a Type Editor . . . . . . . . . . . . . . . .
Creating the GradientEditor . . . . . . . . . . . . .
Implementing the AngleEditor . . . . . . . . . . .
Applying the Custom Editor with the EditorAttribute . . . . .
Defining a Windows Forms Designer . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . .

Chapter 10

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

262
262
263
263
263
268
269
269
272
278
278
279
280
281
282
282
283
287
291
291
293
296
297
298

Creating Custom Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Demonstrated Topics . . . . . . . . . . . . .

Defining the Custom Attribute Class . . . . . .
Implementing an Attribute Constructor . . . .
Positional Arguments . . . . . . . . .
Named Arguments . . . . . . . . . .
Adding Other Members to Custom Attributes .
Applying the AttributeUsageAttribute . . . . .
Specifying Attribute Targets . . . . . .
Specifying If Your Attribute Is Inherited

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

302

303
303
303
304
305
306
306
306

xv


xvi

Advanced C# Programming

Specifying If Your Attribute Can Be Applied Multiple Times
Reading Attributes . . . . . . . . . . . . . . . . . . . . . . .
Secondary Topics . . . . . . . . . . . . . . . . . . . . . . . .
Commenting Attributes . . . . . . . . . . . . . . . . . . . . .
Implementing Extender Provider . . . . . . . . . . . . . . . .
Specifying the Class Header for the HelpProvider . . . .
Applying the ProviderPropertyAttribute . . . . . . . . .
Implement the IExtenderProvider Interface . . . . . . .
Implement the HelpProvider . . . . . . . . . . . . . .
Using the HelpProvider . . . . . . . . . . . . . . . . .
Reviewing the EditorBrowsableAttribute . . . . . . . . . . . . .
Reviewing the DesignerSerializationVisibilityAttribute . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . .


Chapter 11

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

306
307
309
309
311
311
312
312
313
314
315
316
318


Practical Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Demonstrated Topics . . . . . . . . . . . . . . . . . . . . . . . . .
Discovering and Using Types Dynamically . . . . . . . . . . . . . . .
Using Type Objects . . . . . . . . . . . . . . . . . . . . . .
Instantiating Objects with Reflection . . . . . . . . . . . . . .
Dynamic Member Invocation . . . . . . . . . . . . . . . . .
Loading Assemblies Using Reflection . . . . . . . . . . . . .
Generating User Interfaces with Reflected Objects . . . . . . .
Exploring the .NET Framework with Reflection . . . . . . . . . . . .
Returning to the CLR Reference Application, Assembly Viewer 2
Requesting Type Information . . . . . . . . . . . . . . . . .
Reflecting Methods and Method Parameters . . . . . . . . . .
Accessing Properties and Fields Dynamically . . . . . . . . . .
Binding to Events Dynamically . . . . . . . . . . . . . . . .
Emitting Dynamic Assemblies . . . . . . . . . . . . . . . . . . . . .
Emitting Assemblies in Memory . . . . . . . . . . . . . . . .
Saving Emitted Assemblies to File . . . . . . . . . . . . . . .
Using Builder Objects to Create IL . . . . . . . . . . . . . . .
Writing Lines of OpCodes . . . . . . . . . . . . . . . . . . .
Loading and Executing Dynamic Assemblies . . . . . . . . . .
Secondary Topics . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reflection and Web Services . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

320
320
321
322

323
331
333
337
338
338
341
343
347
348
349
352
353
355
362
363
363


Contents

Implementing the Metaclass Idiom . .
Serializing Objects . . . . . . . . . .
Emitting Regular Expression Assemblies
Summary . . . . . . . . . . . . . .

Part III
Chapter 12

.

.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

364
365
366
367

Web Applications—IBUYSPY Portal
Implementing Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Demonstrated Topics . . . . . . . . . . . . . .
Web Services: Discovery and Description . . . .
DISCO Is Back . . . . . . . . . . . . .
WSDL . . . . . . . . . . . . . . . . .
Consuming Web Services . . . . . . . .
Reviewing Web Services Wire Formats .
Testing Web Services . . . . . . . . . . . . . .
Creating a Simple Web Service . . . . . . . . .
Returning Simple Data . . . . . . . . .
Providing a Namespace and Description .
Handling Web Service Exceptions . . . .
Invoking Web Services Asynchronously .
Returning Complex Data from a Web Service . .
Returning a DataSet from a Web Service . . . .

Secondary Topics . . . . . . . . . . . . . . . .
Using the SoapFormatter . . . . . . . .
Implementing the ISerializable Interface
Summary . . . . . . . . . . . . . . . . . . .

Chapter 13

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

372
372
374
375
376
378
379
380
380
384
384
385
388
392
393
393
394
397

IBuySpy and Dynamic User Interfaces in ASP.NET . . . . . . . . . . . . . 399

Demonstrated Topics . . . . . . . .
Creating a Cascading Style Sheet . .
Navigating Style Sheets . . .
Defining Elements . . . . .
Defining Classes . . . . . .
Using Elements and Classes .
Implementing the Portal Banner . .
Designing User Controls . .
Working with the Design View

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

400
401
401
405
406
406
408
408
409

xvii



xviii

Advanced C# Programming

Programming in the Code-Behind Module
Secondary Topics . . . . . . . . . . . . . . . .
Administrating the Portal . . . . . . . . . . . .
Debugging the IBUYSPY Portal . . . . . . . . .
Introducing Mobile Modules . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . .

Chapter 14

.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.

.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

415
432
432
434
434
434

Creating Custom Web Controls . . . . . . . . . . . . . . . . . . . . . . . . 435
Demonstrated Topics . . . . . . . . . . . . . . . . . . . . . .
Rendering Controls Dynamically . . . . . . . . . . . . . . . .
Creating a Custom Server Control . . . . . . . . . . . .

Incorporating the Custom Server Control into a Web Page
Saving Control State . . . . . . . . . . . . . . . . . .
Creating a Composite Custom Control . . . . . . . . . .
Using the WebControl Library Project Template . . . . .
Reviewing Custom Controls in the Portal . . . . . . . .
Reviewing the PortalModuleControl Base Control . . . . . . . .
Exploring Base Control Properties . . . . . . . . . . . .
Using Control Attributes . . . . . . . . . . . . . . . . .
Binding the Tabs Data . . . . . . . . . . . . . . . . . . . . .
Visually Designing the SignIn Module . . . . . . . . . . . . . .
Creating the Image Module . . . . . . . . . . . . . . . . . . .
Creating the Links Module . . . . . . . . . . . . . . . . . . .
Coding Script Blocks in the HTML Code . . . . . . . . .
Creating Script Blocks Using the Template Editor . . . .
Implementing the XML/XSL Transform Module . . . . . . . . .
Reviewing the XML Document . . . . . . . . . . . . .
Reviewing the XSL Document . . . . . . . . . . . . . .
Loading the XML and XSL Documents with the Xml Control
Creating a Custom Portal Module . . . . . . . . . . . . . . . .
Creating a Custom Portal Module . . . . . . . . . . . .
Integrating the Custom Module into the IBuySpy Portal .
Secondary Topics . . . . . . . . . . . . . . . . . . . . . . . .
Using the HttpServerUtility . . . . . . . . . . . . . . . . . . .
Using the Repeater Control . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

436
436
437
438
440
444
446
446
446
447
447
447
449
451
452
453
456
457
457

459
460
462
462
463
465
466
467
468


Contents

Chapter 15

Output Caching and Persisting State Information . . . . . . . . . . . . . . 469
Demonstrated Topics . . . . . . . . . . . . . . . .
Output Caching Pages . . . . . . . . . . . . . . .
Using Declarative Caching . . . . . . . . .
Caching Examples in the IBuySpy Portal . .
Using Programmatic Caching . . . . . . . .
Caching Partial Pages . . . . . . . . . . . . . . .
Caching Data . . . . . . . . . . . . . . . . . . . .
Portal Caching . . . . . . . . . . . . . . .
Adding Data to the Cache . . . . . . . . . .
Creating a File Dependency . . . . . . . . .
Using the Session Cache . . . . . . . . . . . . . .
Caching Data in the Session . . . . . . . . .
Caching Objects in the Session Cache . . . .
Managing the Session Cache . . . . . . . .

Using the Application Cache . . . . . . . . . . . .
Secondary Topics . . . . . . . . . . . . . . . . . .
Configuring the Session State Server . . . . . . . .
Configuring the SQL Server for Session Management
Summary . . . . . . . . . . . . . . . . . . . . .

Chapter 16

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

470
471
471
482
482
484
486
487
493
493
495
496

497
500
500
500
501
503
504

Security and Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Demonstrated Topics . . . . . . . . . . . . . . . . . . . . . .
Using Windows NTLM Authentication . . . . . . . . . . . . . .
Modifying the Web.config File for Windows Authentication
Denying and Allowing Authorization . . . . . . . . . .
Implementing Forms Authentication with Cookies . . . . . . . .
Modifying the Web.config File for Forms Authentication .
Authenticating Users . . . . . . . . . . . . . . . . . .
Programming with Cookies . . . . . . . . . . . . . . .
Implementing the SignIn Module . . . . . . . . . . . . . . . .
Administering Users . . . . . . . . . . . . . . . . . . . . . .
Secondary Topics . . . . . . . . . . . . . . . . . . . . . . . .
Implementing Code Access Security . . . . . . . . . . . . . . .
Declarative Security . . . . . . . . . . . . . . . . . .
Imperative Security . . . . . . . . . . . . . . . . . . .

Index

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

506
507
507
508

509
509
511
516
520
521
522
523
525
525

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529

xix


This page intentionally left blank.


Acknowledgments

I

would like to thank Wendy Rinaldi and Katie Conley at McGraw-Hill/Osborne. There
is a tremendous amount of work that goes into a book, and even more that goes into
books with lots of code. Without their vision and efforts, I wouldn’t have had the
opportunity to write this book. I hope I gave as much as I got.
Thanks to David Fugate at Waterside for proactive stewardship and for taking care of the
nuts-and-bolts stuff that makes it possible for me to write. Special thanks to Mary Bonnici
who has taken professional care of all financial matters without fail for many years now.

Special thanks to Susan Warren and Rob Howard at Microsoft. Susan and Rob provided
invaluable assistance by reviewing the outline, providing timely and proactive assistance on
the tough questions, and coordinating all of the IBUYSPY coverage. Thank you Susan and Rob.
Thanks to Steve Balmer, who has made it a policy for all of the busy Microsoft employees
to drop what they are doing to help customers and authors. Clearly, the program managers
and developers take Steve seriously. As far as I know, no one was directly compensated for
helping me with technical questions, and my total experience demonstrates Microsoft’s clear
commitment to developers, to Microsoft .NET, and to the authors and publishers trying to
bring developers the highest quality of information possible. Thanks to all of the program
managers and developers who answered dozens of questions cheerfully and quickly.
I would like to thank Lewis Gouge. Mr. Gouge played matchmaker between me and
Multnomah County ISD. Building an enterprise system using .NET for the corrections
department was a rewarding challenge. Thanks to Steve “Mr. Ed” Chennault, Mark “Ploki”
Davis, Bill Arnold, Joe “Bilbo” Shook, Robert Phillips, Brook Riddick, Yvette Yutze, Jeff
Braunstein, Peggy Duerscherl, Richard “Jelly” Augelli, Eric “Hank Hill” Cotter, John
“Ballpeen” Armitage, Kathy Erwin, Geoff Caylor, and Karen Britton. I enjoyed working
with and learning from so many smart people in one place.
Thanks to Yonnie, Erin, and Larinda at Wynne’s in Portland, Oregon for food and adult
beverages.
Finally, thank you to my family—Lori, Alex, Douglas, Noah, and Trevor. The only thing I
enjoy more than building software and writing is spending time with you guys. Home truly is
where my heart is.

Copyright 2002 by The McGraw-Hill Companies, Inc. Click Here for Terms of Use.

xxi


This page intentionally left blank.



Introduction

A

dvanced C# Programming is about programmers and code. This book was
conceived around the idea of providing a lot of code listings for programmers
who need to solve problems now.
In each chapter you will find a brief introduction presenting the program showcased in
the chapter and describing the kinds of problems that you may encounter and how the code
presented will help you tackle them. From there, you can go right to the complete code listing
to find the solutions, then read the explanations that follow describing the technology that
supports the code listing.
Because each code listing is a complete application, you will find several applications that
demonstrate both primary and secondary capabilities of C# and Microsoft .NET.
Advanced C# Programming was written for developers who have an intermediate to
advanced level of experience with similar languages, such as C++, Delphi, or Visual Basic
.NET, or who have read an introductory-level book explaining the fundamentals of objectoriented programming with C#.
As a special feature, Chapters 12 through 16 demonstrate the advanced aspects of ASP.NET
programming for the Web with C#. I am pleased to tell you that these chapters of Advanced
C# Programming showcase the IBUYSPY.COM Web site as the application for demonstrating
software development concepts for the Web. We could not have done this without the blessing
and able assistance of Microsoft and Susan Warren, program manager for ASP.NET.
The code examples in this book will demonstrate how to use the most beneficial and
powerful aspects of C# programming for Windows and the Web. Everything you need to
know about Reflection, Assemblies, object-oriented programming, security and authentication,
and Web Services and Web applications for e-commerce sites can be found within this book.
All you have to do is open it up and begin exploring.

Copyright 2002 by The McGraw-Hill Companies, Inc. Click Here for Terms of Use.


xxiii


×