Professional C# 6 and .neT Core 1.0
inTroduCTion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . li
➤ ParT i The C# language
ChaPTer 1 .NET Application Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
ChaPTer 2 Core C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
ChaPTer 3 Objects and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
ChaPTer 4 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
ChaPTer 5 Managed and Unmanaged Resources . . . . . . . . . . . . . . . . . . . . . . 123
ChaPTer 6 Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
ChaPTer 7 Arrays and Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
ChaPTer 8 Operators and Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
ChaPTer 9 Delegates, Lambdas, and Events . . . . . . . . . . . . . . . . . . . . . . . . . . 243
ChaPTer 10 Strings and Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
ChaPTer 11 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
ChaPTer 12 Special Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
ChaPTer 13 Language Integrated Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
ChaPTer 14 Errors and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
ChaPTer 15 Asynchronous Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
ChaPTer 16 Reflection, Metadata, and Dynamic Programming . . . . . . . . . . . . 417
➤ ParT ii .neT Core and WindoWs runTime
ChaPTer 17 Visual Studio 2015 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
ChaPTer 18 .NET Compiler Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
ChaPTer 19 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
ChaPTer 20 Diagnostics and Application Insights . . . . . . . . . . . . . . . . . . . . . . . 569
ChaPTer 21 Tasks and Parallel Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
ChaPTer 22 Task Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
ChaPTer 23 Files and Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
Continues
ChaPTer 24 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
ChaPTer 25 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
ChaPTer 26 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
ChaPTer 27 XML and JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 781
ChaPTer 28 Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
➤ ParT iii WindoWs aPPs
ChaPTer 29 Core XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849
ChaPTer 30 Styling XAML Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 871
ChaPTer 31 Patterns with XAML Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923
ChaPTer 32 Windows Apps: User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 951
ChaPTer 33 Advanced Windows Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 991
ChaPTer 34 Windows Desktop Applications with WPF . . . . . . . . . . . . . . . . . . 1035
ChaPTer 35 Creating Documents with WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . 1101
ChaPTer 36 Deploying Windows Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1125
➤ ParT iv Web aPPliCaTions and serviCes
ChaPTer 37 ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1143
ChaPTer 38 Entity Framework Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1159
ChaPTer 39 Windows Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1189
ChaPTer 40 ASP.NET Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1219
ChaPTer 41 ASP.NET MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1255
ChaPTer 42 ASP.NET Web API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1311
ChaPTer 43 WebHooks and SignalR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1337
ChaPTer 44 Windows Communication Foundation . . . . . . . . . . . . . . . . . . . . . 1357
ChaPTer 45 Deploying Websites and Services . . . . . . . . . . . . . . . . . . . . . . . . . 1401
index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1417
Professional
C# 6 and .neT Core 1.0
Professional
C# 6 and .neT Core 1.0
Christian Nagel
Professional C# 6 and .neT Core 1.0
Published by John Wiley & Sons, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com
Copyright © 2016 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-119-09660-3
ISBN: 978-1-119-09671-9 (ebk)
ISBN: 978-1-119-09663-4 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or pro-motional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the pub-lisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2016932153
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trade-marks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book.
http://www.wiley.comhttp://www.wiley.com/go/permissionshttp://booksupport.wiley.comhttp://www.wiley.com
This book is dedicated to my family—Angela,
Stephanie, and Matthias—I love you all!
abouT The auThor
ChrisTian nagel is Microsoft MVP for Visual Studio and Development Technologies, and has been Microsoft Regional Director for more than 15 years. Christian is an associate of thinktecture and founder of CN innovation, where he offers training and consulting on how to develop solutions using the Microsoft platform. He draws on more than 25 years of software development experience.
Christian started his computing career with PDP 11 and VAX/VMS systems at Digital Equipment Corporation, covering a variety of languages and platforms. Since 2000, when .NET was just a technology preview, he has been working with various technologies to build .NET solutions. Currently, he mainly coaches people
on development of Universal Windows Platform apps and ASP.NET MVC, using several Microsoft Azure service offerings.
Even after many years in software development, Christian still loves learning and using new technologies and teaching others how to use the new technologies in various forms. Using his profound knowledge of Microsoft technologies, he has written numerous books, and is certified as Microsoft Certified Trainer and Certified Solution Developer. Christian speaks at international conferences such as TechEd, BASTA!, and TechDays. He founded INETA Europe to support .NET user groups. You can contact Christian via his website www.cninnovation.com and follow his tweets at @christiannagel.
http://www.cninnovation.com
abouT The TeChniCal ediTor
isTván novák is an associate and the chief technology consultant with SoftwArt, a small Hungarian IT consulting company. He works as a software architect and community evangelist. In the last 25 years, he has participated in more than 50 enterprise software development projects. In 2002, he coauthored the first Hungarian book about .NET development. In 2007, he was awarded the Microsoft Most Valuable Professional (MVP) title, and in 2011 he became a Microsoft Regional Director. István coauthored Visual Studio 2010 and .NET 4 Six-in-One (Wiley, 2010) and Beginning Windows 8 Application Development (Wiley, 2012), and he authored Beginning Visual Studio LightSwitch Development (Wiley, 2011).
István holds master’s degree from the Technical University of Budapest, Hungary and also has a doctoral degree in software technology. He lives in Dunakeszi, Hungary, with his wife and two daughters. He is a passionate scuba diver. You may have a good chance of meeting him underwater in the Red Sea, any season of the year.
senior aCquisiTions ediTorKen Brown
ProjeCT ediTorCharlotte Kughen
TeChniCal ediTorIstván Novák
ProduCTion ediTorDassi Zeidel
manager of ConTenT develoPmenT & assemblyMary Beth Wakefield
ProduCTion manager Kathleen Wisor
markeTing direCTorDavid Mayhew
markeTing managerCarrie Sherrill
Professional TeChnology & sTraTegy direCTorBarry Pruett
business managerAmy Knies
assoCiaTe PublisherJim Minatel
ProjeCT CoordinaTor, CoverPatrick Redmond
ProofreaderAmy J. Schneider
indexerJohn Sleeva
Cover designerWiley
Cover image© Digital Storn/Shutterstock
CrediTs
aCknoWledgmenTs
i WanT To Thank Charlotte Kughen, who made my text so much more readable. Often I was working late at night writing while .NET Core was continuously evolving. Charlotte was of enormous help to change my ideas into great readable text. I’m almost sure that Charlotte now knows a lot more about programming than she would like to. Special thanks also goes to István Novák, who has authored several great books. Despite all the issues we had with the fast-evolving .NET Core and the interim builds I was using while working on the book, István challenged me to enhance the code samples that allow you—the reader—to better follow the flow. Thank you, Charlotte and István—you’ve been of great help for the quality of this book.
I also would like to thank Kenyon Brown and Jim Minatel and everyone else at Wiley who helped to get edition 10 of this great book to be published. I also want to thank my wife and children for supporting my writing. You understood and helped with the time I was working on the book, including evenings, nights, and weekends. Angela, Stephanie, and Matthias—you are my loved ones. This would not have been possible without you.
Contents
IntroductIon li
Part I: the C# Language
ChaPter 1: .net aPPLICatIon arChIteCtures 3
Choosing Your Technologies 3Reviewing .NET History 4
C# 1.0—A New Language 4C# 2 and .NET 2 with Generics 5.NET 3—Windows Presentation Foundation 6C# 3 and .NET 3.5—LINQ 6C# 4 and .NET 4—Dynamic and TPL 7C# 5 and Asynchronous Programming 7C# 6 and .NET Core 8Choosing Technologies and Going Forward 9
.NET 2015 10.NET Framework 4.6 10.NET Core 1.0 11Assemblies 12
Private Assemblies 12Shared Assemblies 13
NuGet Packages 13Common Language Runtime 14.NET Native 15Windows Runtime 15
Hello, World 16Compiling with .NET 4.6 17Compiling with .NET Core CLI 19
Setting Up the Environment 19Building the Application 19Packaging and Publishing the Application 22
Application Types and Technologies 23Data Access 23Windows Desktop Applications 24Universal Windows Platform 25SOAP Services with WCF 25
xviii
CONTENTS
Web Services with the ASP.NET Web API 26WebHooks and SignalR 26Windows Services 26Web Applications 27Microsoft Azure 27
Software as a Service 27Infrastructure as a Service 27Platform as a Service 27
Developer Tools 28Visual Studio Community 28Visual Studio Professional with MSDN 28Visual Studio Enterprise with MSDN 28Visual Studio Code 29
Summary 29
ChaPter 2: Core C# 31
Fundamentals of C# 32Creating Hello, World! with Visual Studio 32
Creating a Solution 32Creating a New Project 33Compiling and Running the Program 35Taking a Closer Look at the Code 37
Working with Variables 38Initializing Variables 39Using Type Inference 39Understanding Variable Scope 40
Scope Clashes for Local Variables 41Scope Clashes for Fields and Local Variables 42
Working with Constants 42Using Predefined Data Types 43
Value Types and Reference Types 43.NET Types 44Predefined Value Types 44
Integer Types 44Floating-Point Types 45The Decimal Type 46The Boolean Type 46The Character Type 46
Predefined Reference Types 47The object Type 47The string Type 47
xix
CONTENTS
Controlling Program Flow 49Conditional Statements 49
The if Statement 49The switch Statement 51
Loops 52The for Loop 52The while Loop 54The do. . .while Loop 54The foreach Loop 55
Jump Statements 55The goto Statement 55The break Statement 56The continue Statement 56The return Statement 56
Working with Enumerations 56Getting Organized with Namespaces 57
The using Directive 58Namespace Aliases 59
Understanding the Main Method 60Using Comments 61
Internal Comments Within the Source Files 61XML Documentation 62
Understanding C# Preprocessor Directives 63#define and #undef 63#if, #elif, #else, and #endif 64#warning and #error 65#region and #endregion 65#line 65#pragma 65
C# Programming Guidelines 66Rules for Identifiers 66Usage Conventions 67
Naming Conventions 68Use of Properties and Methods 70Use of Fields 70
Summary 71
ChaPter 3: objeCts and tyPes 73
Creating and Using Classes 74Classes and Structs 74Classes 75
xx
CONTENTS
Fields 76Properties 76
Auto-Implemented Properties 77Access Modifiers for Properties 77
Methods 78Declaring Methods 78Expression-Bodied Methods 79Invoking Methods 79Method Overloading 80Named Arguments 80Optional Arguments 81Variable Number of Arguments 82
Constructors 82Calling Constructors from Other Constructors 84Static Constructors 85
Readonly Members 87Readonly Fields 87Readonly Properties 88Auto-implemented Readonly Properties 88Expression-Bodied Properties 88Immutable Types 89
Anonymous Types 89Structs 90
Structs Are Value Types 91Structs and Inheritance 92Constructors for Structs 92
Passing Parameters by Value and by Reference 92ref Parameters 93out Parameters 94
Nullable Types 94Enumerations 95Partial Classes 97Extension Methods 99The Object Class 100Summary 101
ChaPter 4: InherItanCe 103
Inheritance 103Types of Inheritance 103
Multiple Inheritance 104Structs and Classes 104
xxi
CONTENTS
Implementation Inheritance 104Virtual Methods 105Polymorphism 107Hiding Methods 107Calling Base Versions of Methods 109Abstract Classes and Methods 109Sealed Classes and Methods 110Constructors of Derived Classes 111
Modifiers 113Access Modifiers 113Other Modifiers 114
Interfaces 115Defining and Implementing Interfaces 116Interface Inheritance 118
is and as Operators 120Summary 121
ChaPter 5: Managed and unManaged resourCes 123
Resources 123Memory Management Under the Hood 124
Value Data Types 124Reference Data Types 127Garbage Collection 129
Strong and Weak References 131Working with Unmanaged Resources 132
Destructors or Finalizers 133The IDisposable Interface 134The using Statement 135Implementing IDisposable and a Destructor 135IDisposable and Finalizer Rules 137
Unsafe Code 137Accessing Memory Directly with Pointers 138
Writing Unsafe Code with the unsafe Keyword 139Pointer Syntax 140Casting Pointers to Integer Types 141Casting Between Pointer Types 142void Pointers 142Pointer Arithmetic 142The sizeof Operator 144Pointers to Structs: The Pointer Member Access Operator 144Pointers to Class Members 145
xxii
CONTENTS
Pointer Example: PointerPlayground 146Using Pointers to Optimize Performance 150
Creating Stack-based Arrays 150QuickArray Example 153
Platform Invoke 154Summary 158
ChaPter 6: generICs 159
Generics Overview 159Performance 160Type Safety 161Binary Code Reuse 162Code Bloat 162Naming Guidelines 162
Creating Generic Classes 162Generics Features 166
Default Values 167Constraints 167Inheritance 170Static Members 171
Generic Interfaces 171Covariance and Contra-variance 172Covariance with Generic Interfaces 172Contra-Variance with Generic Interfaces 174
Generic Structs 174Generic Methods 177
Generic Methods Example 177Generic Methods with Constraints 178Generic Methods with Delegates 179Generic Methods Specialization 180
Summary 181
ChaPter 7: arrays and tuPLes 183
Multiple Objects of the Same and Different Types 183Simple Arrays 184
Array Declaration 184Array Initialization 184Accessing Array Elements 185Using Reference Types 186
Multidimensional Arrays 187Jagged Arrays 188
xxiii
CONTENTS
Array Class 189Creating Arrays 189Copying Arrays 190Sorting 191
Arrays as Parameters 194Array Covariance 194ArraySegment 194
Enumerators 195IEnumerator Interface 196foreach Statement 196yield Statement 197
Different Ways to Iterate Through Collections 199Returning Enumerators with Yield Return 200
Tuples 201Structural Comparison 202Summary 204
ChaPter 8: oPerators and Casts 205
Operators and Casts 205Operators 206
Operator Shortcuts 207The Conditional Operator (?:) 208The checked and unchecked Operators 209The is Operator 210The as Operator 210The sizeof Operator 210The typeof Operator 211The nameof Operator 211The index Operator 212Nullable Types and Operators 212The Null Coalescing Operator 213The Null Propagation Operator 214Operator Precedence and Associativity 215
Type Safety 216Type Conversions 217
Implicit Conversions 217Explicit Conversions 218
Boxing and Unboxing 220Comparing Objects for Equality 221
Comparing Reference Types for Equality 221The ReferenceEquals Method 221
xxiv
CONTENTS
The Virtual Equals Method 221The Static Equals Method 221Comparison Operator (==) 222
Comparing Value Types for Equality 222Operator Overloading 222
How Operators Work 223Operator Overloading Example: The struct Vector 224Overloading the Comparison Operators 228Which Operators Can You Overload? 230
Implementing Custom Index Operators 231User-Defined Casts 233
Implementing User-Defined Casts 234Casts Between Classes 237Casts Between Base and Derived Classes 238Boxing and Unboxing Casts 239
Multiple Casting 239Summary 242
ChaPter 9: deLegates, LaMbdas, and events 243
Referencing Methods 243Delegates 244
Declaring Delegates 245Using Delegates 246Simple Delegate Example 249Action and Func Delegates 250BubbleSorter Example 251Multicast Delegates 253Anonymous Methods 256
Lambda Expressions 258Parameters 258Multiple Code Lines 259Closures 259
Events 260Event Publisher 260Event Listener 262Weak Events 263
Summary 265
ChaPter 10: strIngs and reguLar exPressIons 267
Examining System.String 268Building Strings 269StringBuilder Members 272
xxv
CONTENTS
String Formats 273String Interpolation 273
FormattableString 273Using Other Cultures with String Interpolation 274Escaping Curly Brackets 275
DateTime and Number Formats 275Custom String Formats 276
Regular Expressions 277Introduction to Regular Expressions 278The RegularExpressionsPlayground Example 279Displaying Results 282Matches, Groups, and Captures 283
Summary 285
ChaPter 11: CoLLeCtIons 287
Overview 287Collection Interfaces and Types 288Lists 289
Creating Lists 290Collection Initializers 290Adding Elements 291Inserting Elements 291Accessing Elements 292Removing Elements 292Searching 293Sorting 295Read-Only Collections 296
Queues 296Stacks 300Linked Lists 302Sorted List 306Dictionaries 308
Dictionary Initializers 308Key Type 309Dictionary Example 310Lookups 313Sorted Dictionaries 314
Sets 314Performance 316Summary 318
xxvi
CONTENTS
ChaPter 12: sPeCIaL CoLLeCtIons 319
Overview 319Working with Bits 319
BitArray 320BitVector32 322
Observable Collections 324Immutable Collections 326
Using Builders with Immutable Collections 328Immutable Collection Types and Interfaces 328Using LINQ with Immutable Arrays 329
Concurrent Collections 329Creating Pipelines 330Using a BlockingCollection 333Using a ConcurrentDictionary 334Completing the Pipeline 335
Summary 336
ChaPter 13: Language Integrated Query 339
LINQ Overview 339Lists and Entities 340LINQ Query 343Extension Methods 343Deferred Query Execution 345
Standard Query Operators 347Filtering 348Filtering with Index 349Type Filtering 349Compound from 350Sorting 351Grouping 352Variables Within the LINQ Query 352Grouping with Nested Objects 353Inner Join 354Left Outer Join 355Group Join 356Set Operations 359Zip 360Partitioning 361Aggregate Operators 362Conversion Operators 363Generation Operators 365
xxvii
CONTENTS
Parallel LINQ 365Parallel Queries 365Partitioners 366Cancellation 367
Expression Trees 367LINQ Providers 370Summary 370
ChaPter 14: errors and exCePtIons 371
Introduction 371Exception Classes 372Catching Exceptions 374
Implementing Multiple Catch Blocks 376Catching Exceptions from Other Code 379System.Exception Properties 379Exception Filters 380Re-throwing Exceptions 381
Naïve Use to Rethrow the Exception 382Changing the Exception 383Rethrowing the Exception 384Using Filters to Add Functionality 384
What Happens If an Exception Isn’t Handled? 385User-Defined Exception Classes 385
Catching the User-Defined Exceptions 386Throwing the User-Defined Exceptions 388Defining the User-Defined Exception Classes 391
Caller Information 393Summary 394
ChaPter 15: asynChronous PrograMMIng 395
Why Asynchronous Programming Is Important 395Asynchronous Patterns 396
Synchronous Call 402Asynchronous Pattern 403Event-Based Asynchronous Pattern 405Task-Based Asynchronous Pattern 405
Foundation of Asynchronous Programming 407Creating Tasks 408Calling an Asynchronous Method 408Continuation with Tasks 409Synchronization Context 409
xxviii
CONTENTS
Using Multiple Asynchronous Methods 409Calling Asynchronous Methods Sequentially 409Using Combinators 410
Converting the Asynchronous Pattern 410Error Handling 411
Handling Exceptions with Asynchronous Methods 412Handling Exceptions with Multiple Asynchronous Methods 412Using AggregateException Information 413
Cancellation 414Starting a Cancellation 414Cancellation with Framework Features 415Cancellation with Custom Tasks 415
Summary 416
ChaPter 16: refLeCtIon, Metadata, and dynaMIC PrograMMIng 417
Inspecting Code at RunTime and Dynamic Programming 417Custom Attributes 418
Writing Custom Attributes 419Specifying the AttributeUsage Attribute 420Specifying Attribute Parameters 421Specifying Optional Attribute Parameters 421
Custom Attribute Example: WhatsNewAttributes 422The WhatsNewAttributes Library Assembly 422The VectorClass Assembly 423
Using Reflection 425The System.Type Class 425
Type Properties 425Methods 426
The TypeView Example 427The Assembly Class 429
Getting Details About Types Defined in an Assembly 430Getting Details About Custom Attributes 430
Completing the WhatsNewAttributes Example 431Using Dynamic Language Extensions for Reflection 435
Creating the Calculator Library 435Instantiating a Type Dynamically 436Invoking a Member with the Reflection API 438Invoking a Member with the Dynamic Type 439
The Dynamic Type 440Dynamic Behind the Scenes 441
Top Related