Tải bản đầy đủ (.pdf) (1,801 trang)

pro vb 2010 and the .net 4.0 platform

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 (24.76 MB, 1,801 trang )

this print for content only—size & color not accurate
Troelsen
Agarwal
  CYAN
  MAGENTA
  YELLOW
  BLACK
  PANTONE 123 C
Companion
eBook Available
BOOKS FOR PROFESSIONALS BY PROFESSIONALS
®
Pro VB 2010 and the .NET 4
Platform
Dear Reader,
With the core VB language stable, the 4th edition of the .NET platform has
brought with it a massive wealth of new functionality. The new edition of this
book is your guide to learning the ins and outs of the .NET 4 platform and
Visual Studio 2010, including:
• the Dynamic Language Runtime (DLR)
• the Task Parallel Library (TPL, including PLINQ)
• the completely revamped WF 4 API
• the ADO.NET Entity Framework (as well as LINQ to EF)
• the expansion of the Windows Presentation Foundation (WPF) API
• improved COM interop
If you’re checking out this book for the first time, understand that it targets
experienced software professionals and students of computer science—so
please don’t expect three chapters devoted to “variable scope” here! The
mission of this text is to provide you with a rock-solid foundation in the VB
programming language (including a full discussion of OOP) and the critical
aspects of the .NET 4 platform.


Once you digest the information presented in the book you hold in your
hands, you’ll be in a perfect position to apply this knowledge to your specific
programming assignments, and you’ll be well-equipped to explore the .NET
universe on your own terms.
Andrew Troelsen and Vidya Vrat Agarwal
US $59.99
Shelve in:
Programming/VB
User level:
Intermediate–Advanced
THE APRESS ROADMAP
Pro
ASP.NET 4 in VB 2010
Introducing
.NET 4.0
Pro
Windows Azure
Pro Dynamic
.NET 4.0 Applications
Pro
VB 2010 and the
.NET 4 Platform
Pro
WPF in VB 2010
www.apress.com
SOURCE CODE ONLINE
Companion eBook

See last page for details
on $10 eBook version

ISBN 978-1-4302-2985-8
9 781430 229858
5 59 9 9
Vidya Vrat Agarwal, Author of
Pro ASP.NET 3.5 in VB 2008
Beginning C# 2008 Databases
Beginning VB 2008 Databases
spine = 2.15625" 1800 page count 840 PPI
THE EXPERT’S VOICE
®
 IN .NET
Pro
VB 2010
.NET 4 Platform
Andrew Troelsen and Vidya Vrat Agarwal
Exploring the .NET universe with
Visual Basic 2010
and
the
VB
Pro
2010
and the
.NET 4
Platform
Andrew Troelsen, Author of
Pro C# 2010 and the .NET 4
Platform
Pro VB 2008 and the .NET 3.5
Platform

Pro Expression Blend 4
Expert ASP.NET 2.0
Advanced Application Design
www.it-ebooks.info
www.it-ebooks.info


Pro VB 2010 and
the .NET 4 Platform











■ ■ ■
Andrew Troelsen
and Vidya Vrat Agarwal


www.it-ebooks.info

Pro VB 2010 and the .NET 4 Platform
Copyright © 2010 by Andrew Troelsen and Vidya Vrat Agarwal
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,

electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-4302-2985-8
ISBN-13 (electronic): 978-1-4302-2986-5
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of
the trademark owner, with no intention of infringement of the trademark.
President and Publisher: Paul Manning
Lead Editor: Ewan Buckingham
Technical Reviewer: Andy Olsen
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell,
Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes,
Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft,
Matt Wade, Tom Welsh
Coordinating Editor: Debra Kelly
Copy Editors: Mary Behr, Patrick Meader, Katie Stence, and Sharon Terdeman
Production Support: Patrick Cunningham
Indexer: BIM Indexing & Proofreading Services
Artist: April Milne
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail , or
visit www.springeronline.com.
For information on translations, please e-mail , or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our
Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales.
The information in this book is distributed on an “as is” basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have

any liability to any person or entity with respect to any loss or damage caused or alleged to be caused
directly or indirectly by the information contained in this work.
The source code for this book is available to readers at www.apress.com.
www.it-ebooks.info
To the memory of my wonderful grandmother, Maxine. I love you, Gerta.
Rest peacefully.
–Andrew Troelsen
To my lovely wife, sweet daughters, and my parents. I love you all.
–Vidya Vrat Agarwal






































www.it-ebooks.info

iv
Contents at a Glance
About the Authors liv
About the Technical Reviewer lv
Acknowledgments lvi
Introduction lvii

■Chapter 1: Introducing VB 2010 1
■Chapter 2: Building Visual Basic 2010 Applications 41
■Chapter 3: Core VB 2010 Programming Constructs, Part I 71
■Chapter 4: Core VB 2010 Programming Constructs, Part II 125
■Chapter 5: Defining Encapsulated Class Types 169

■Chapter 6: Understanding Inheritance and Polymorphism 227
■Chapter 7: Understanding Structured Exception Handling 273
■Chapter 8: Understanding Object Lifetime 303
■Chapter 9: Working with Interfaces 333
■Chapter 10: Understanding Generics 375
■Chapter 11: Delegates, Events, and Lambdas 415
■Chapter 12: Advanced VB 2010 Language Features 461
■Chapter 13: LINQ to Objects 507
■Chapter 14: Configuring .NET Assemblies 543
■Chapter 15: Type Reflection, Late Binding,
■and Attribute-Based Programming 605
www.it-ebooks.info
■ CONTENTS AT A GLANCE
v
■Chapter 16: Processes, AppDomains, and Object Contexts 653
■Chapter 17: Understanding CIL and the Role of Dynamic Assemblies 687
■Chapter 18: Dynamic Types and the Dynamic Language Runtime 739
■Chapter 19: Multithreaded and Parallel Programming 765
■Chapter 20: File I/O and Object Serialization 817
■Chapter 21: ADO.NET Part I: The Connected Layer 869
■Chapter 22: ADO.NET Part II: The Disconnected Layer 931
■Chapter 23: ADO.NET Part III: The Entity Framework 999
■Chapter 24: Introducing LINQ to XML 1041
■Chapter 25: Introducing Windows Communication Foundation 1061
■Chapter 26: Introducing Windows Workflow Foundation 4.0 1127
■Chapter 27: Introducing Windows Presentation Foundation and XAML 1165
■Chapter 28: Programming with WPF Controls 1227
■Chapter 29: WPF Graphics Rendering Services 1295
■Chapter 30: WPF Resources, Animations, and Styles 1335
■Chapter 31: WPF Control Templates and UserControls 1377

■Chapter 32: Building ASP.NET Web Pages 1427
■Chapter 33: ASP.NET Web Controls, Master Pages, and Themes 1477
■Chapter 34: ASP.NET State Management Techniques 1523
■Appendix A: Programming with Windows Forms 1561
■Appendix B: Platform-Independent .NET Development with Mono 1613

Index 1635
www.it-ebooks.info

vi
Contents
About the Authors liv
About the Technical Reviewer lv
Acknowledgments lvi
Introduction lvii

■Chapter 1: Introducing VB 2010 1
Understanding the Previous State of Affairs 1
Life As a C/Windows API Programmer 1
Life As a C++/MFC Programmer 2
Life As a Visual Basic 6.0 Programmer 2
Life As a Java Programmer 3
Life As a COM Programmer 3
The .NET Solution 4
Introducing the Building Blocks of the .NET Platform (the CLR, CTS, and CLS) 5
The Role of the Base Class Libraries 6
What Visual Basic 2010 Brings to the Table 6
Additional .NET-Aware Programming Languages 8
Life in a Multi-Language World 10
An Overview of .NET Assemblies 10

Single-File and Multi-File Assemblies 12
The Role of the Common Intermediate Language 12
The Role of .NET Type Metadata 15
The Role of the Assembly Manifest 17
www.it-ebooks.info
■ CONTENTS
vii
Understanding the Common Type System 18
CTS Class Types 18
CTS Interface Types 19
CTS Structure Types 19
CTS Enumeration Types 20
CTS Delegate Types 20
CTS Type Members 21
Intrinsic CTS Data Types 21
Understanding the Common Language Specification 22
Ensuring CLS Compliance 24
Understanding the Common Language Runtime 24
The Assembly/Namespace/Type Distinction 26
The Role of the Microsoft Root Namespace 29
Accessing a Namespace Programmatically 29
Referencing External Assemblies 31
Exploring an Assembly Using ildasm.exe 32
Viewing CIL Code 33
Viewing Type Metadata 34
Viewing Assembly Metadata (aka the Manifest) 34
Exploring an Assembly Using Reflector 35
Deploying the .NET Runtime 37
The .NET Client Profile Runtime 37
The Platform-Independent Nature of .NET 37

Summary 39
■Chapter 2: Building Visual Basic 2010 Applications 41
The Role of the .NET Framework 4.0 SDK 41
The Visual Studio 2010 Command Prompt 42
www.it-ebooks.info
■ CONTENTS
viii
Building VB 2010 Applications Using vbc.exe 43
Specifying Input and Output Targets 44
Referencing External Assemblies 45
Referencing Multiple External Assemblies 46
Compiling Multiple Source Files 46
Working with VB 2010 Response Files 47
Building .NET Applications Using Notepad++ 49
Building .NET Applications Using Visual Basic 2010 Express 50
Some Unique Features of Visual Basic 2010 Express 51
Building .NET Applications Using Visual Studio 2010 52
Some Unique Features of Visual Studio 2010 53
Targeting the .NET Framework Using the New Project Dialog Box 53
Using the Solution Explorer Utility 54
The Class View Utility 57
The Object Browser Utility 58
Integrated Support for Code Refactoring 59
Code Expansions Techniques 61
The Visual Class Designer 62
The Integrated .NET Framework 4.0 SDK Documentation System 68
Summary 70
■Chapter 3: Core VB 2010 Programming Constructs, Part I 71
The Role of the Module Type 71
Projects with Multiple Modules 73

Modules Are Not Creatable 74
Renaming Your Initial Module 75
Members of Modules 75
www.it-ebooks.info
■ CONTENTS
ix
The Anatomy of a Simple VB 2010 Program 76
Variations on the Main() Method 77
Specifying an Application Error Code 78
Processing Command-Line Arguments 80
Specifying Command-Line Arguments with Visual Studio 2010 81
An Interesting Aside: Some Additional Members of the System.Environment Class 82
The System.Console Class 84
Basic Input and Output with the Console Class 85
Formatting Console Output 86
Formatting Numerical Data 86
Formatting Numerical Data Beyond Console Applications 88
System Data Types and VB 2010 Shorthand Notation 89
Variable Declaration and Initialization 91
Intrinsic Data Types and the New Operator 92
The Data Type Class Hierarchy 93
Members of Numerical Data Types 95
Members of System.Boolean 96
Members of System.Char 96
Parsing Values from String Data 97
System.DateTime and System.TimeSpan 98
The .NET 4.0 System.Numerics Namespace 98
Working with String Data 100
Basic String Manipulation 101
String Concatenation 102

Special Character Constants 103
End SubStrings and Equality 104
Strings Are Immutable 105
The System.Text.StringBuilder Type 106
www.it-ebooks.info
■ CONTENTS
x
Narrowing and Widening Data Type Conversions 108
Trapping Narrowing Data Conversions . 111
Understanding Option Strict . 112
Setting Project-wide Overflow Checking . 113
The Role of System.Convert . 114
Understanding Implicitly Typed Local Variables 115
Restrictions on Implicitly Typed Variables . 116
Implicit Typed Data Is Strongly Typed Data . 117
Usefulness of Implicitly Typed Local Variables . 118
VB 2010 Iteration Constructs 118
The For Loop . 119
The For Each Loop . 119
The While and Do/While Looping Constructs . 120
Decision Constructs and the Relational/Equality Operators . 121
The If/Then/Else Statement . 121
The Select/Case Statement 123
Summary 124
■Chapter 4: Core VB 2010 Programming Constructs, Part II . 125
Methods and Parameter Modifiers . 125
The ByVal Parameter Modifier . 126
The ByRef Modifier . 128
The <Out()> Attribute . 129
The ParamArray Modifier . 130

Defining Optional Parameters . 132
Invoking Methods using Named Parameters 133
Understanding Method Overloading . 135
Download from Wow! eBook <www.wowebook.com>
www.it-ebooks.info
■ CONTENTS
xi
Understanding VB 2010 Arrays 138
VB 2010 Array Initialization Syntax 139
Implicitly Typed Local Arrays 139
Defining an Array of Objects 140
Working with Multidimensional Arrays 141
Arrays As Arguments or Return Values 143
The System.Array Base Class 144
Understanding the Enum Type 146
Controlling the Underlying Storage for an Enum 147
Declaring Enum Variables 147
The System.Enum Type 148
Dynamically Discovering an Enum’s Name/Value Pairs 149
Understanding the Structure Type 152
Creating Structure Variables 153
Understanding Value Types and Reference Types 155
Value Types, References Types, and the Assignment Operator 156
Value Types Containing Reference Types 158
Passing Reference Types by Value 160
Passing Reference Types by Reference 162
Final Details Regarding Value Types and Reference Types 163
Understanding VB 2010 Nullable Types 164
Working with Nullable Types 165
The Nullable If Operator 166

Summary 167
■Chapter 5: Defining Encapsulated Class Types 169
Introducing the VB 2010 Class Type 169
Allocating Objects with the New Keyword 172
www.it-ebooks.info
■ CONTENTS
xii
Understanding Constructors 173
The Role of the Default Constructor 173
Defining Custom Constructors 174
The Default Constructor Revisited 175
The Role of the Me Keyword 176
Chaining Constructor Calls Using Me 178
Observing Constructor Flow 181
Revisiting Optional Arguments 183
Understanding the Shared Keyword 184
Defining Shared Methods 184
Defining Shared Field Data 185
Defining Shared Constructors 188
Defining Modules in VB 2010 190
Defining the Pillars of OOP 193
The Role of Encapsulation 193
The Role of Inheritance 194
The Role of Polymorphism 195
VB 2010 Access Modifiers 197
The Default Access Modifiers 197
Access Modifiers and Nested Types 198
The First Pillar: VB 2010 Encapsulation Services 198
Encapsulation Using Traditional Accessors and Mutators 199
Encapsulation Using .NET Properties 202

Using Properties within a Class Definition 206
Internal Representation of Properties 207
Controlling Visibility Levels of Property Get/Set Statements 210
Read-Only and Write-Only Properties 210
Shared Properties 211
www.it-ebooks.info
■ CONTENTS
xiii
Understanding Automatic Properties 213
Interacting with Automatic Properties 214
Regarding Automatic Properties and Default Values 215
Understanding Object Initializer Syntax 217
Calling Custom Constructors with Initialization Syntax 218
Nested Objects 220
Working with Constant Field Data 221
Understanding Read-Only Fields 222
Shared Read-Only Fields 223
Understanding Partial Types 224
Summary 225
■Chapter 6: Understanding Inheritance and Polymorphism 227
The Basic Mechanics of Inheritance 227
Specifying the Parent Class of an Existing Class 228
Regarding Multiple Base Classes 230
The NotInheritable Keyword 230
Revising Visual Studio Class Diagrams 232
The Second Pillar of OOP: The Details of Inheritance 235
Controlling Base Class Creation with the MyBase Keyword 237
Keeping Family Secrets: The Protected Keyword 239
Adding a NotInheritable Class 240
Programming for Containment/Delegation 241

Understanding Nested Type Definitions 243
The Third Pillar of OOP: VB’s Polymorphic Support 245
The Overridable and Overrides Keywords 246
Overriding Overridable Members Using Visual Studio 2010 249
NotOverridable Members 250
Understanding Abstract Classes and the MustInherit Keyword 250
www.it-ebooks.info
■ CONTENTS
xiv
Understanding the Polymorphic Interface 252

Understanding Member Shadowing 257
Understanding Base Class/Derived Class Casting Rules 259
The VB TryCast Keyword 262
The VB Is Keyword 262
The Master Parent Class: System.Object 263
Overriding System.Object.ToString() 267
Overriding System.Object.Equals() 268
Overriding System.Object.GetHashCode() 269
Testing Your Modified Person Class 270
The Shared Members of System.Object 271
Summary 272
■Chapter 7: Understanding Structured Exception Handling 273
Ode to Errors, Bugs, and Exceptions 273
The Role of .NET Exception Handling 274
The Atoms of .NET Exception Handling 275
The System.Exception Base Class 276
The Simplest Possible Example 277
Throwing a General Exception 280
Catching Exceptions 281

Configuring the State of an Exception 283
The TargetSite Property 283
The StackTrace Property 284
The HelpLink Property 284
The Data Property 285
System-Level Exceptions (System.SystemException) 288
www.it-ebooks.info
■ CONTENTS
xv
Application-Level Exceptions (System.ApplicationException) 288
Building Custom Exceptions, Take One 289
Building Custom Exceptions, Take Two 291
Building Custom Exceptions, Take Three 292
Processing Multiple Exceptions 293
General Catch Statements 295
Rethrowing Exceptions 296
Inner Exceptions 296
The Finally Block 297
Who Is Throwing What? 298
The Result of Unhandled Exceptions 298
Debugging Unhandled Exceptions Using Visual Studio 299
A Brief Word Regarding Corrupted State Exceptions (CSE) 300
Summary 302
■Chapter 8: Understanding Object Lifetime 303
Classes, Objects, and References 303
The Basics of Object Lifetime 305
The CIL of new 306
Setting Object References to Nothing 307
The Role of Application Roots 308
Understanding Object Generations 310

Concurrent Garbage Collection under .NET 1.0 - 3.5 311
Background Garbage Collection under .NET 4.0 311
The System.GC Type 312
Forcing a Garbage Collection 313
www.it-ebooks.info
■ CONTENTS
xvi
Building Finalizable Objects 316
Overriding System.Object.Finalize() 317
Detailing the Finalization Process 319
Building Disposable Objects 320
Using Keyword with VB 2010 322
Building Finalizable and Disposable Types 324
A Formalized Disposal Pattern 325
Understanding Lazy Object Instantiation 327
Customizing the Creation of the Lazy Data 330
Summary 331
■Chapter 9: Working with Interfaces 333
Understanding Interface Types 333
Interface Types vs. Abstract Base Classes 335
Defining Custom Interfaces 337
Implementing an Interface 339
Invoking Interface Members at the Object Level 342
Obtaining Interface References: The TryCast Keyword 343
Obtaining Interface References: The Is Keyword 343
Interfaces As Parameters 345
Interfaces As Return Values 348
Arrays of Interface Types 348
Implementing Interfaces Using Visual Studio 2010 350
Resolving Name Clashes via Explicit Interface Implementation 351

Designing Interface Hierarchies 354
Multiple Inheritance with Interface Types 356
Building Enumerable Types (IEnumerable and IEnumerator) 358
www.it-ebooks.info
■ CONTENTS
xvii
Building Cloneable Objects (ICloneable) 361
A More Elaborate Cloning Example 363
Building Comparable Objects (IComparable) 367
Specifying Multiple Sort Orders (IComparer) 371
Custom Properties, Custom Sort Types 373
Summary 373
■Chapter 10: Understanding Generics 375
The Issues with Non-Generic Collections 375
The Issue of Performance 377
The Issue of Type Safety 381
The Role of Generic Type Parameters 386
Specifying Type Parameters for Generic Classes / Structures 387
Specifying Type Parameters for Generic Members 389
Specifying Type Parameters for Generic Interfaces 389
The System.Collections.Generic Namespace 391
Understanding Collection Initialization Syntax 392
Working with the List(Of T) Class 394
Working with the Stack(Of T) Class 397
Working with the Queue(Of T) Class 398
Working with the SortedSet(Of T) Class 400
Creating Custom Generic Methods 402
Inference of Type Parameters 405
Creating Custom Generic Structures and Classes 406
The Nothing Keyword in Generic Code 407

Generic Base Classes 409
www.it-ebooks.info
■ CONTENTS
xviii
Constraining Type Parameters 410
Examples Using the As Keyword 411
The Lack of Operator Constraints 412
Summary 413
■Chapter 11: Delegates, Events, and Lambdas 415
Understanding the .NET Delegate Type 415
Defining a Delegate Type in VB 2010 416
The System.MulticastDelegate and System.Delegate Base Classes 419
The Simplest Possible Delegate Example 421
Investigating a Delegate Object 422
Sending Object State Notifications Using Delegates 424
Enabling Multicasting 428
Removing Targets from a Delegate’s Invocation List 429
Method Group Conversion Syntax 431
Understanding Delegate Covariance 432
Understanding Generic Delegates 435
Simulating Generic Delegates Without Generics 436
Understanding VB 2010 Events 437
The VB 2010 Event Keyword 439
Events under the Hood 440
Listening to Incoming Events 442
Simplifying Event Registration Using Visual Studio 2010 444
Creating Custom Event Arguments 445
The Generic EventHandler(Of T) Delegate 446
Understanding VB 2010 Anonymous Methods 447
Accessing Local Variables 449

www.it-ebooks.info
■ CONTENTS
xix
Understanding Lambda Expressions 450
Dissecting a Lambda Expression 453
Processing Arguments Within Multiple Statements 454
Lambda Expressions with Multiple (or Zero) Parameters 456
Retrofitting the CarEvents Example Using Lambda Expressions 457
Summary 459
■Chapter 12: Advanced VB 2010 Language Features 461
Understanding Indexer Methods 461
Indexing Data Using String Values 464
Overloading Indexer Methods 465
Indexers with Multiple Dimensions 466
Indexer Definitions on Interface Types 467
Understanding Operator Overloading 467
Overloading Binary Operators 468
And What of the += and –= Operators? 470
Overloading Unary Operators 471
Overloading Equality Operators 472
Overloading Comparison Operators 473
The Internal Representation of Overloaded Operators 474
Final Thoughts Regarding Operator Overloading 476
Understanding Custom Type Conversions 477
Recall: Numerical Conversions 477
Recall: Conversions Among Related Class Types 478
Creating Custom Conversion Routines 479
Additional Explicit Conversions for the Square Type 482
Defining Implicit Conversion Routines 482
The Internal Representation of Custom Conversion Routines 484

Understanding Extension Methods 485
www.it-ebooks.info
■ CONTENTS
xx
Understanding Partial Methods . 496
Understanding Anonymous Types . 498
Summary 505
■Chapter 13: LINQ to Objects 507
LINQ Specific Programming Constructs 507
Implicit Typing of Local Variables . 508
Object and Collection Initialization Syntax. 509
Lambda Expressions. 509
Extension Methods . 510
Anonymous Types . 511
Understanding the Role of LINQ 511
LINQ Expressions Are Strongly Typed 513
The Core LINQ Assemblies . 513
Applying LINQ Queries to Primitive Arrays 514
Once Again, Without LINQ 515
Reflecting Over a LINQ Result Set . 516
LINQ and Implicitly Typed Local Variables . 517
LINQ and Extension Methods . 519
The Role of Deferred Execution . 520
The Role of Immediate Execution . 521
Returning the Result of a LINQ Query . 522
Returning LINQ Results via Immediate Execution . 523
Applying LINQ Queries to Collection Objects . 524
Accessing Contained Subobjects . 525
Applying LINQ Queries to Nongeneric Collections . 526
Filtering Data Using OfType(Of T)() . 527

Download from Wow! eBook <www.wowebook.com>
www.it-ebooks.info
■ CONTENTS
xxi
Investigating the VB 2010 LINQ Query Operators 527
Basic Selection Syntax 529
Obtaining Subsets of Data 530
Projecting New Data Types 531
Obtaining Counts Using Enumerable 532
Reversing Result Sets 533
Sorting Expressions 533
LINQ As a Better Venn Diagramming Tool 534
Removing Duplicates 536
LINQ Aggregation Operations 536
The Internal Representation of LINQ Query Statements 537
Building Query Expressions with Query Operators (Revisited) 538
Building Query Expressions Using the Enumerable Type and Lambda Expressions 538
Building Query Expressions Using the Enumerable Type and Anonymous Methods 540
Building Query Expressions Using the Enumerable Type and Raw Delegates 541
Summary 542
■Chapter 14: Configuring .NET Assemblies 543
Observing the Root Namespace 543
Defining Namespaces Beyond the Root 544
Defining Custom Namespaces 545
Resolving Name Clashes with Fully Qualified Names 547
Resolving Name Clashes with Aliases 548
Creating Nested Namespaces 550
The Role of .NET Assemblies 551
Assemblies Promote Code Reuse 551
Assemblies Establish a Type Boundary 552

Assemblies Are Versionable Units 552
Assemblies Are Self-Describing 552
Assemblies Are Configurable 552
www.it-ebooks.info
■ CONTENTS
xxii
Understanding the Format of a .NET Assembly 553
The Windows File Header 553
The CLR File Header 555
CIL Code, Type Metadata, and the Assembly Manifest 556
Optional Assembly Resources 557
Single-File and Multifile Assemblies 557
Building and Consuming a Single-File Assembly 559
Exploring the Manifest 563
Exploring the CIL 566
Exploring the Type Metadata 567
Building a VB 2010 Client Application 568
Building a C# Client Application 570
Cross-Language Inheritance in Action 571
Building and Consuming a Multifile Assembly 572
Exploring the ufo.netmodule File 573
Exploring the airvehicles.dll File 573
Consuming a Multifile Assembly 574
Understanding Private Assemblies 575
The Identity of a Private Assembly 576
Understanding the Probing Process 576
Configuring Private Assemblies 577
Configuration Files and Visual Studio 2010 579
Understanding Shared Assemblies 582
Understanding Strong Names 583

Generating Strong Names at the Command Line 585
Generating Strong Names using Visual Studio 2010 587
Installing Strongly Named Assemblies to the GAC 588
Consuming a Shared Assembly 591
Exploring the Manifest of SharedCarLibClient 592
www.it-ebooks.info
■ CONTENTS
xxiii
Configuring Shared Assemblies 593
Freezing the Current Shared Assembly 593
Building Shared Assembly Version 2.0.0.0 594
Dynamically Redirecting to Specific Versions of a Shared Assembly 598
Understanding Publisher Policy Assemblies 599
Disabling Publisher Policy 600
Understanding the <codeBase> Element 600
The System.Configuration Namespace 603
Summary 604
■Chapter 15: Type Reflection, Late Binding,
■and Attribute-Based Programming 605
The Necessity of Type Metadata 605
Viewing (Partial) Metadata for the EngineState Enumeration 606
Viewing (Partial) Metadata for the Car Type 607
Examining a TypeRef 609
Documenting the Defining Assembly 609
Documenting Referenced Assemblies 609
Documenting String Literals 610
Understanding Reflection 611
The System.Type Class 612
Obtaining a Type Reference Using System.Object.GetType() 613
Obtaining a Type Reference Using System.Type.GetType() 613

Obtaining a Type Reference Using GetType() 614
Building a Custom Metadata Viewer 614
Reflecting on Methods 614
Reflecting on Fields and Properties 615
Reflecting on Implemented Interfaces 616
Displaying Various Odds and Ends 616
www.it-ebooks.info

×