Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Pro CSharp 2008 And The .NET 3.5 Platform [eng]

.pdf
Скачиваний:
71
Добавлен:
16.08.2013
Размер:
22.5 Mб
Скачать

x CONTENTS

Part 5 ■ ■ ■ Introducing the .NET Base Class Libraries

CHAPTER 20 File I/O and Isolated Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661

Exploring the System.IO Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661

The Directory(Info) and File(Info) Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662

Working with the DirectoryInfo Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663

Working with the Directory Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667

Working with the DriveInfo Class Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668

Working with the FileInfo Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669

Working with the File Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673

The Abstract Stream Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675

Working with StreamWriters and StreamReaders . . . . . . . . . . . . . . . . . . . . . . . . 677

Working with StringWriters and StringReaders . . . . . . . . . . . . . . . . . . . . . . . . . . 680

Working with BinaryWriters and BinaryReaders . . . . . . . . . . . . . . . . . . . . . . . . . . 682

Programmatically “Watching” Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683

Performing Asynchronous File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685

Understanding the Role of Isolated Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687

A Primer on Code Access Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688

An Overview of Isolated Storage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698

Obtaining a Store Using IsolatedStorageFile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702

Isolated Storage in Action: ClickOnce Deployment. . . . . . . . . . . . . . . . . . . . . . . . 707

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710

CHAPTER 21 Introducing Object Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

711

Understanding Object Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711

Configuring Objects for Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713

Choosing a Serialization Formatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715

Serializing Objects Using the BinaryFormatter . . . . . . . . . . . . . . . . . . . . . . . . . . . 717

Serializing Objects Using the SoapFormatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719

Serializing Objects Using the XmlSerializer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720

Serializing Collections of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723

Customizing the Serialization Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729

CHAPTER 22 ADO.NET Part I: The Connected Layer. . . . . . . . . . . . . . . . . . . . . . . . . . . . 731

A High-Level Definition of ADO.NET. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731

Understanding ADO.NET Data Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733

Additional ADO.NET Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736

The Types of the System.Data Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737

Abstracting Data Providers Using Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741

Creating the AutoLot Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744

The ADO.NET Data Provider Factory Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749

Understanding the Connected Layer of ADO.NET. . . . . . . . . . . . . . . . . . . . . . . . . 755

Working with Data Readers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760

Building a Reusable Data Access Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763

CONTENTS xi

Creating a Console UI–Based Front End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770

Asynchronous Data Access Using SqlCommand . . . . . . . . . . . . . . . . . . . . . . . . . 775

Understanding Database Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 781

CHAPTER 23 ADO.NET Part II: The Disconnected Layer . . . . . . . . . . . . . . . . . . . . . . . . 783

Understanding the Disconnected Layer of ADO.NET . . . . . . . . . . . . . . . . . . . . . . 783

Understanding the Role of the DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784

Working with DataColumns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787

Working with DataRows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789

Working with DataTables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793

Binding DataTable Objects to User Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . 798

Filling DataSet/DataTable Objects Using Data Adapters . . . . . . . . . . . . . . . . . . . 808

Revisiting AutoLotDAL.dll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811

Navigating Multitabled DataSet Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814

The Data Access Tools of Visual Studio 2008. . . . . . . . . . . . . . . . . . . . . . . . . . . . 820

Decoupling Autogenerated Code from the UI Layer . . . . . . . . . . . . . . . . . . . . . . . 831

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835

CHAPTER 24 Programming with the LINQ APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837

The Role of LINQ to ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837

Programming with LINQ to DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838

Programming with LINQ to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843

Generating Entity Classes Using SqlMetal.exe . . . . . . . . . . . . . . . . . . . . . . . . . . . 848

Building Entity Classes Using Visual Studio 2008. . . . . . . . . . . . . . . . . . . . . . . . . 854

Manipulating XML Documents Using LINQ to XML. . . . . . . . . . . . . . . . . . . . . . . . 857

Navigating an In-Memory Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 865

CHAPTER 25 Introducing Windows Communication Foundation. . . . . . . . . . . . . . . 867

A Potpourri of Distributed Computing APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867

The Role of WCF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873

Investigating the Core WCF Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876

The Visual Studio WCF Project Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 877

The Basic Composition of a WCF Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879

The ABCs of WCF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880

Building a WCF Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885

Hosting the WCF Service. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888

Building the WCF Client Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 896

Using the WCF Service Library Project Template . . . . . . . . . . . . . . . . . . . . . . . . . 900

Hosting the WCF Service As a Windows Service . . . . . . . . . . . . . . . . . . . . . . . . . 903

Invoking a Service Asynchronously . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 908

Designing WCF Data Contracts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 910

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915

xii CONTENTS

CHAPTER 26 Introducing Windows Workflow Foundation . . . . . . . . . . . . . . . . . . . . .

917

Defining a Business Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917

The Building Blocks of WF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 918

WF Assemblies, Namespaces, and Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924

Building a Simple Workflow-Enabled Application. . . . . . . . . . . . . . . . . . . . . . . . . 926

Examining the WF Engine Hosting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930

Invoking Web Services Within Workflows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934

Building a Reusable WF Code Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945

A Brief Word Regarding Custom Activities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 952

Part 6 ■ ■ ■ Desktop User Interfaces

CHAPTER 27 Programming with Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

955

The Windows Forms Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955

Building a Simple Windows Forms Application (IDE-Free). . . . . . . . . . . . . . . . . . 956

The Visual Studio Windows Forms Project Template . . . . . . . . . . . . . . . . . . . . . . 961

The Anatomy of a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968

Responding to Mouse Activity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975

Responding to Keyboard Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977

Designing Dialog Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978

Rendering Graphical Data Using GDI+. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985

Building a Complete Windows Forms Application . . . . . . . . . . . . . . . . . . . . . . . . 990

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997

CHAPTER 28 Introducing Windows Presentation Foundation and XAML . . . . . . . 999

The Motivation Behind WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 999

The Various Flavors of WPF Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002

Investigating the WPF Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1004

Building a (XAML-Free) WPF Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1011

Additional Details of the Application Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1015

Additional Details of the Window Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1017

Building a (XAML-Centric) WPF Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1021

Transforming Markup into a .NET Assembly. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025

Separation of Concerns Using Code-Behind Files . . . . . . . . . . . . . . . . . . . . . . . 1029

The Syntax of XAML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1031

Building WPF Applications Using Visual Studio 2008. . . . . . . . . . . . . . . . . . . . . 1044

Processing XAML at Runtime: SimpleXamlPad.exe . . . . . . . . . . . . . . . . . . . . . . 1048

The Role of Microsoft Expression Blend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1052

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1053

CONTENTS xiii

CHAPTER 29 Programming with WPF Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1055

A Survey of the WPF Control Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1055

Declaring Controls in XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058

Understanding the Role of Dependency Properties . . . . . . . . . . . . . . . . . . . . . . 1060

Understanding Routed Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1064

Working with Button Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1068

Working with CheckBoxes and RadioButtons . . . . . . . . . . . . . . . . . . . . . . . . . . . 1072

Working with the ListBox and ComboBox Types . . . . . . . . . . . . . . . . . . . . . . . . 1075

Working with Text Areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1081

Controlling Content Layout Using Panels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083

Building a Window’s Frame Using Nested Panels . . . . . . . . . . . . . . . . . . . . . . . 1093

Understanding WPF Control Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1099

Understanding the WPF Data-Binding Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 1102

Data Conversion Using IValueConverter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1106

Binding to Custom Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1108

Binding UI Elements to XML Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1112

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1116

CHAPTER 30 WPF 2D Graphical Rendering, Resources, and Themes . . . . . . . . .

1117

The Philosophy of WPF Graphical Rendering Services. . . . . . . . . . . . . . . . . . . . 1117

Exploring the Shape-Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1124

Working with WPF Brushes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1127

Working with WPF Pens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1130

Exploring the Drawing-Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1130

The Role of UI Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1135

Understanding WPF’s Animation Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1136

Understanding the WPF Resource System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1145

Defining and Applying Styles for WPF Controls . . . . . . . . . . . . . . . . . . . . . . . . . 1147

Altering a Control’s UI Using Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1156

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1160

Part 7 ■ ■ ■ Building Web Applications with ASP.NET

CHAPTER 31 Building ASP.NET Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1163

The Role of HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1163

Understanding Web Applications and Web Servers . . . . . . . . . . . . . . . . . . . . . . 1164

The Role of HTML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1167

The Role of Client-Side Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1172

Submitting the Form Data (GET and POST) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1174

Building a Classic ASP Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175

Problems with Classic ASP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1177

The ASP.NET Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1178

The ASP.NET Web Page Code Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1179

Details of an ASP.NET Website Directory Structure . . . . . . . . . . . . . . . . . . . . . . 1190

The ASP.NET Page Compilation Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1192

xiv CONTENTS

The Inheritance Chain of the Page Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1194

Interacting with the Incoming HTTP Request . . . . . . . . . . . . . . . . . . . . . . . . . . . 1195

Interacting with the Outgoing HTTP Response . . . . . . . . . . . . . . . . . . . . . . . . . . 1198

The Life Cycle of an ASP.NET Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1200

The Role of the Web.config File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1203

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1206

CHAPTER 32 ASP.NET Web Controls, Themes, and Master Pages . . . . . . . . . . . .

1207

Understanding the Nature of Web Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1207

The System.Web.UI.Control Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1209

The System.Web.UI.WebControls.WebControl Type. . . . . . . . . . . . . . . . . . . . . . 1213

Major Categories of ASP.NET Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1213

Building a Feature-Rich ASP.NET Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1215

The Role of the Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1231

Working with Themes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1237

Positioning Controls Using HTML Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1243

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1244

CHAPTER 33 ASP.NET State Management Techniques . . . . . . . . . . . . . . . . . . . . . . . 1245

The Issue of State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1245

ASP.NET State Management Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1247

Understanding the Role of ASP.NET View State . . . . . . . . . . . . . . . . . . . . . . . . . 1248

The Role of the Global.asax File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1251

Understanding the Application/Session Distinction . . . . . . . . . . . . . . . . . . . . . . 1254

Working with the Application Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1259

Maintaining Session Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1263

Understanding Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1267

The Role of the <sessionState> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1269

Understanding the ASP.NET Profile API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1272

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1279

Part 8 ■ ■ ■ Appendixes

APPENDIX A COM and .NET Interoperability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1283

The Scope of .NET Interoperability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1283

A Simple Example of .NET to COM Interop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1284

Investigating a .NET Interop Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1287

Understanding the Runtime Callable Wrapper . . . . . . . . . . . . . . . . . . . . . . . . . . 1289

The Role of COM IDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1292

Using a Type Library to Build an Interop Assembly . . . . . . . . . . . . . . . . . . . . . . 1296

Building a More Elaborate COM Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1299

Examining the Interop Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1301

Understanding COM to .NET Interoperability. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1305

The Role of the CCW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1306

The Role of the .NET Class Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1307

CONTENTS xv

 

Building Your .NET Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1308

 

Generating the Type Library and Registering the .NET Types. . . . . . . . . . . . . .

1310

 

Examining the Exported Type Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1311

 

Building a Visual Basic 6.0 Test Client. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1312

 

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1313

APPENDIX B

Platform-Independent .NET Development with Mono . . . . . . . . . . .

1315

 

The Platform-Independent Nature of .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1315

 

Obtaining and Installing Mono . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1318

 

The Mono Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1321

 

Building .NET Applications with Mono . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1323

 

Suggestions for Further Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1330

 

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1331

INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1333

About the Author

ANDREW TROELSEN is a Microsoft MVP (Visual C#) and a partner, trainer, and consultant with Intertech Training (http://www.Intertech.com), a .NET and J2EE developer education center. He is the author of numerous books, including Developer’s Workshop to COM and ATL 3.0 (Wordware Publishing, 2000), COM and .NET Interoperability (Apress, 2002), Visual Basic .NET and the .NET Platform: An Advanced Guide (Apress, 2001), and the award-winning C# and the .NET Platform (Apress, 2003). Andrew has also authored numerous articles on .NET for MSDN online, DevX, and MacTech, and is frequently a speaker at

various .NET conferences and user groups.

Andrew lives in Minneapolis, Minnesota, with his wife, Amanda. He spends his free time waiting for the Wild to win the Stanley Cup, but has given up all hope of the Vikings winning a Super Bowl and feels quite strongly that the Timberwolves will never get back to the playoffs until current management is replaced.

xvi

About the Technical Reviewer

GAVIN SMYTH is a professional software engineer with more years of experience in development than he cares to admit on projects ranging from device drivers to distributed web applications; under platforms as diverse as 8-bit “bare metal,” embedded real-time operating systems, Unix, and Windows; and in languages including assembler, C++, Ada, and C#, among a good many others. He has worked for clients such as BT and Nortel, and is currently employed by Microsoft. Gavin has published a few pieces of technical prose in the past (EXE, where are you now?) but finds criticizing other people’s work much more fulfilling. Beyond that, when he’s not battling weeds and ants in the garden, he tries to persuade LEGO robots to do what he wants them to do (it’s for the kids’ benefit— honest).

xvii

Acknowledgments

While I might be the only name seen on the front of this book, this text would never be printed without the aid of numerous talented people. Allow me to offer some heartfelt words of thanks to the many, many people who made this book possible.

First and foremost, thanks to all of the people at Apress, whom I have had the pleasure of working with for many years now. You are all extremely talented people who do a wonderful job of transforming my original Word documents into polished prose. Thank you so much. Looking forward to working with you all on the next book (well, after I take a sanity break from this book).

Special thanks to my technical editor, Gavin, who has offered me many words of wisdom that I feel make this edition of the book better than ever. As always, any remaining typos or technical errors are my responsibility alone.

Last but not least, thanks to my family, friends, and coworkers who put up with my occasional grumpy demeanor, which sadly presented itself once or twice during the final phases of this manuscript.

xviii

8bbe9bd62dce7511c66cccbd759f8afe

Introduction

This book has existed (in one form or another) since the first edition of C# and the .NET Platform was published in conjunction with the release of .NET 1.0 Beta 2, circa the summer of 2001. Since that point, I have been extremely happy and grateful to see that this text was very well received by the press and, most important, by readers. Over the years it was nominated as a Jolt Award finalist (I lost . . . crap!) and for the 2003 Referenceware Excellence Award in the programming book category (I won? Cool!).

Since that point, I have worked to keep the book current with each release of the .NET platform, including a limited printing of a Special Edition, which introduced the technologies of .NET 3.0 (Windows Presentation Foundation, Windows Communication Foundation, and Windows Workflow Foundation) as well as offered previews of several forthcoming technologies, which we now know as LINQ.

The fourth edition of this text, which you hold in your hands, is a massive retelling of the previous manuscript to account for all of the major changes that are found within .NET 3.5. Not only will you find numerous brand-new chapters, you will find many of the previous chapters have been expanded in great detail.

As with the earlier editions, this edition presents the C# programming language and .NET base class libraries using a friendly and approachable tone. I have never understood the need some technical authors have to spit out prose that reads more like a GRE vocabulary study guide than a readable book. As well, this new edition remains focused on providing you with the information you need to build software solutions today, rather than spending too much time examining esoteric details that few individuals will ever actually care about.

We’re a Team, You and I

Technology authors write for a demanding group of people (I should know—I’m one of them). You know that building software solutions using any platform (.NET, J2EE, COM, etc.) is extremely

detailed and is very specific to your department, company, client base, and subject matter. Perhaps you work in the electronic publishing industry, develop systems for the state or local government, or work at NASA or a branch of the military. Speaking for myself, I have developed children’s educational software, various n-tier systems, and projects within the medical and financial industries. The chances are almost 100 percent that the code you write at your place of employment has little to do with the code I write at mine (unless we happened to work together previously!).

Therefore, in this book, I have deliberately chosen to avoid creating examples that tie the example code to a specific industry or vein of programming. Given this, I explain C#, OOP, the CLR, and the .NET 3.5 base class libraries using industry-agnostic examples. Rather than having every blessed example fill a grid with data, calculate payroll, or whatnot, I’ll stick to subject matter we can all relate to: automobiles (with some geometric structures and employees thrown in for good measure). And that’s where you come in.

My job is to explain the C# programming language and the core aspects of the .NET platform the best I possibly can. As well, I will do everything I can to equip you with the tools and strategies you need to continue your studies at this book’s conclusion.

Your job is to take this information and apply it to your specific programming assignments. I obviously understand that your projects most likely don’t revolve around automobiles with pet

xix