Tài liệu Applied Microsoft .NET Framework Programming docx

441 467 0
Tài liệu Applied Microsoft .NET Framework Programming docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Applied Microsoft .NET Framework Programming Table of Contents Applied Microsoft .NET Framework Programming .1 Introduction 4 What Makes Up the Microsoft .NET Initiative 7 An Underlying Operating System: Windows 7 Helpful Products: The .NET Enterprise Servers .7 Microsoft XML Web Services: .NET My Services 8 The Development Platform: The .NET Framework 8 The Development Environment: Visual Studio .NET .11 Goal of This Book 12 System Requirements 13 This Book Has No Mistakes .13 Support 13 Part I: Basics of the Microsoft .NET Framework .15 Chapter List 15 Chapter 1: The Architecture of the .NET Framework Development Platform 16 Compiling Source Code into Managed Modules 16 Combining Managed Modules into Assemblies .18 Loading the Common Language Runtime .19 Executing Your Assembly’s Code 22 IL and Verification 27 The .NET Framework Class Library .29 The Common Type System .31 The Common Language Specification .33 Interoperability with Unmanaged Code 37 Chapter 2: Building, Packaging, Deploying, and Administering Applications and Types .40 Overview 40 .NET Framework Deployment Goals .40 Building Types into a Module .41 Combining Modules to Form an Assembly 47 Adding Assemblies to a Project Using the Visual Studio .NET IDE .52 Using the Assembly Linker .53 Including Resource Files in the Assembly .55 Assembly Version Resource Information .55 Version Numbers .59 Culture .60 Simple Application Deployment (Privately Deployed Assemblies) 61 Simple Administrative Control (Configuration) .62 Chapter 3: Shared Assemblies .66 Overview 66 Two Kinds of Assemblies, Two Kinds of Deployment 67 Giving an Assembly a Strong Name 67 The Global Assembly Cache .71 The Internal Structure of the GAC 76 Building an Assembly That References a Strongly Named Assembly .78 Strongly Named Assemblies Are Tamper−Resistant .80 i Table of Contents Chapter 3: Shared Assemblies Delayed Signing .81 Privately Deploying Strongly Named Assemblies 84 Side−by−Side Execution 85 How the Runtime Resolves Type References .86 Advanced Administrative Control (Configuration) 88 Publisher Policy Control .93 Repairing a Faulty Application .95 Part II: Working with Types and the Common Language Runtime .99 Chapter List 99 Chapter 4: Type Fundamentals 100 All Types Are Derived from System.Object 100 Casting Between Types .101 Casting with the C# is and as Operators 103 Namespaces and Assemblies 105 Chapter 5: Primitive, Reference, and Value Types .109 Programming Language Primitive Types .109 Checked and Unchecked Primitive Type Operations .112 Reference Types and Values Types 114 Boxing and Unboxing Value Types 118 Chapter 6: Common Object Operations 128 Object Equality and Identity .128 Implementing Equals for a Reference Type Whose Base Classes Don’t Override Object’s Equals .129 Implementing Equals for a Reference Type When One or More of Its Base Classes Overrides Object’s Equals .130 Implementing Equals for a Value Type 131 Summary of Implementing Equals and the ==/!= Operators 133 Identity .134 Object Hash Codes 134 Object Cloning .136 Part III: Designing Types .139 Chapter List 139 Chapter 7: Type Members and Their Accessibility .140 Type Members .140 Accessibility Modifiers and Predefined Attributes 142 Type Predefined Attributes .144 Field Predefined Attributes .144 Method Predefined Attributes .145 Chapter 8: Constants and Fields 147 Constants .147 Fields .148 ii Table of Contents Chapter 9: Methods .150 Instance Constructors 150 Type Constructors 155 Operator Overload Methods 157 Operators and Programming Language Interoperability 159 Conversion Operator Methods .161 Passing Parameters by Reference to a Method 164 Passing a Variable Number of Parameters to a Method 168 How Virtual Methods Are Called 170 Virtual Method Versioning 171 Chapter 10: Properties 176 Parameterless Properties 176 Parameterful Properties .179 Chapter 11: Events 184 Overview 184 Designing a Type That Exposes an Event .185 Designing a Type That Listens for an Event 189 Explicitly Controlling Event Registration 191 Designing a Type That Defines Lots of Events 192 Designing the EventHandlerSet Type 196 Part IV: Essential Types 199 Chapter List 199 Chapter 12: Working with Text .200 Characters .200 The System.String Type 202 Constructing Strings .202 Strings Are Immutable 204 Comparing Strings .205 String Interning .210 String Pooling .213 Examining a String’s Characters 213 Other String Operations .216 Dynamically Constructing a String Efficiently .217 Constructing a StringBuilder Object .217 StringBuilder’s Members 218 Obtaining a String Representation for an Object .220 Specific Formats and Cultures .221 Formatting Multiple Objects into a Single String 224 Providing Your Own Custom Formatter .226 Parsing a String to Obtain an Object .228 Encodings: Converting Between Characters and Bytes 232 Encoding/Decoding Streams of Characters and Bytes 238 Base−64 String Encoding and Decoding .239 iii Table of Contents Chapter 13: Enumerated Types and Bit Flags 240 Enumerated Types .240 Bit Flags .244 Chapter 14: Arrays .247 Overview 247 All Arrays Are Implicitly Derived from System.Array 249 Casting Arrays .251 Passing and Returning Arrays .252 Creating Arrays That Have a Nonzero Lower Bound .253 Fast Array Access 254 Redimensioning an Array .257 Chapter 15: Interfaces .259 Interfaces and Inheritance .259 Designing an Application That Supports Plug−In Components .263 Changing Fields in a Boxed Value Type Using Interfaces .264 Implementing Multiple Interfaces That Have the Same Method 266 Explicit Interface Member Implementations .268 Chapter 16: Custom Attributes .273 Using Custom Attributes 273 Defining Your Own Attribute 276 Attribute Constructor and Field/Property Data Types 278 Detecting the Use of a Custom Attribute 279 Matching Two Attribute Instances Against Each Other 283 Pseudo−Custom Attributes 285 Chapter 17: Delegates .287 A First Look at Delegates .287 Using Delegates to Call Back Static Methods 289 Using Delegates to Call Back Instance Methods .290 Demystifying Delegates .291 Some Delegate History: System.Delegate and System.MulticastDelegate .294 Comparing Delegates for Equality .296 Delegate Chains 296 C#’s Support for Delegate Chains .300 Having More Control over Invoking a Delegate Chain .301 Delegates and Reflection .303 Part V: Managing Types 306 Chapter List 306 Chapter 18: Exceptions .307 Overview 307 The Evolution of Exception Handling .307 The Mechanics of Exception Handling .309 The try Block 310 The catch Block 310 The finally Block .312 iv Table of Contents Chapter 18: Exceptions What Exactly Is an Exception? 312 The System.Exception Class .316 FCL−Defined Exception Classes .317 Defining Your Own Exception Class 319 How to Use Exceptions Properly .322 You Can’t Have Too Many finally Blocks .323 Don’t Catch Everything 324 Gracefully Recovering from an Exception 325 Backing Out of a Partially Completed Operation When an Unrecoverable Exception Occurs .326 Hiding an Implementation Detail 327 What’s Wrong with the FCL .329 Performance Considerations .330 Catch Filters .333 Unhandled Exceptions .335 Controlling What the CLR Does When an Unhandled Exception Occurs 339 Unhandled Exceptions and Windows Forms .340 Unhandled Exceptions and ASP.NET Web Forms 342 Unhandled Exceptions and ASP.NET XML Web Services 342 Exception Stack Traces .342 Remoting Stack Traces 344 Debugging Exceptions .345 Telling Visual Studio What Kind of Code to Debug 349 Chapter 19: Automatic Memory Management (Garbage Collection) 351 Understanding the Basics of Working in a Garbage−Collected Platform .351 The Garbage Collection Algorithm .354 Finalization .357 What Causes Finalize Methods to Get Called .362 Finalization Internals 363 The Dispose Pattern: Forcing an Object to Clean Up 365 Using a Type That Implements the Dispose Pattern 370 C#’s using Statement .373 An Interesting Dependency Issue 374 Weak References 375 Weak Reference Internals 377 Resurrection .378 Designing an Object Pool Using Resurrection .379 Generations .381 Programmatic Control of the Garbage Collector 385 Other Garbage Collector Performance Issues .387 Synchronization−Free Allocations 388 Scalable Parallel Collections 388 Concurrent Collections .389 Large Objects .390 Monitoring Garbage Collections .391 v Table of Contents Chapter 20: CLR Hosting, AppDomains, and Reflection .392 Metadata: The Cornerstone of the .NET Framework .392 CLR Hosting .393 AppDomains 394 Accessing Objects Across AppDomain Boundaries .396 AppDomain Events 397 Applications and How They Host the CLR and Manage AppDomains 398 “Yukon” .399 The Gist of Reflection 400 Reflecting Over an Assembly’s Types .401 Reflecting Over an AppDomain’s Assemblies .403 Reflecting Over a Type’s Members: Binding 404 Explicitly Loading Assemblies 405 Loading Assemblies as “Data Files” .407 Building a Hierarchy of Exception−Derived Types .408 Explicitly Unloading Assemblies: Unloading an AppDomain .410 Obtaining a Reference to a System.Type Object .412 Reflecting Over a Type’s Members 415 Creating an Instance of a Type 417 Calling a Type’s Method .418 Bind Once, Invoke Multiple Times 422 Reflecting Over a Type’s Interfaces .426 Reflection Performance .428 List of Figures 429 List of Tables 432 vi Applied Microsoft .NET Framework Programming Jeffrey Richter PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052−6399 Copyright © 2002 by Jeffrey Richter All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Library of Congress Cataloging−in−Publication Data Richter, Jeffrey. Applied Microsoft .NET Framework Programming / Jeffrey Richter. p. cm. Includes index. ISBN 0−7356−1422−9 1. Microsoft .NET Framework. 2. Internet programming. I. Title. QA76.625 .R53 2002 005.2’76—dc21 2001056250 Printed and bound in the United States of America. 3 4 5 6 7 8 9 QWT 7 6 5 4 3 2 Distributed in Canada by Penguin Books Canada Limited. A CIP catalogue record for this book is available from the British Library. Microsoft Press books are available through booksellers and distributors worldwide. For further information about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936−7329. Visit our Web site at www.microsoft.com/mspress. Send comments to mspinput@microsoft.com. Active Directory, ActiveX, Authenticode, DirectX, IntelliSense, JScript, Microsoft, Microsoft Press, MSDN, the .NET logo, PowerPoint, Visual Basic, Visual C++, Visual Studio, Win32, Windows, and Windows NT are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Other product and company names mentioned herein may be the trademarks of their respective owners. The example companies, organizations, products, domain names, e−mail addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, e−mail address, logo, person, place, or event is intended or should be inferred. Acquisitions Editor: Anne Hamilton Project Editor: Sally Stickney 1 Body Part No. X08−22449 To Kristin I want to tell you how much you mean to me. Your energy and exuberance always lift me higher. Your smile brightens my every day. Your zest makes my heart sing. I love you. Jeffrey Richter Jeffrey Richter is a co−founder of Wintellect (http://www.Wintellect.com/), a training, design, and debugging company dedicated to helping companies produce better software faster. Jeff has written many books, including Programming Applications for Microsoft Windows (Microsoft Press, 1999) and Programming Server−Side Applications for Microsoft Windows 2000 (Microsoft Press, 2000). Jeff is also a contributing editor for MSDN Magazine, where he has written several feature articles and is the .NET columnist. Jeff also speaks at various trade conferences worldwide, including VSLive!, WinSummit, and Microsoft’s TechEd and PDC. Jeff has consulted for many companies, including AT&T, DreamWorks, General Electric, Hewlett−Packard, IBM, and Intel. Jeff’s code has shipped in many Microsoft products, among them Visual Studio, Microsoft Golf, Windows Sound System, and various versions of Windows, from Windows 95 to Windows XP and the Windows .NET Server Family. Since October 1999, Jeff has consulted with the .NET Framework team and has used the .NET Framework to produce the XML Web service front end to Microsoft’s very popular TerraServer Web property (http://www.TerraServer.net/). On the personal front, Jeff holds both airplane and helicopter pilot licenses, though he never gets to fly as often as he’d like. He is also a member of the International Brotherhood of Magicians and enjoys showing friends slight−of−hand card tricks from time to time. Jeff’s other hobbies include music, drumming, and model railroading. He also enjoys traveling and the theater. He lives near Bellevue, Washington, with his wife, Kristin, and their cat, Max. He doesn’t have any children yet, but he has the feeling that kids may be a part of his life soon. Acknowledgments I couldn’t have written this book without the help and technical assistance of many people. In particular, I’d like to thank the following people: Members of the Microsoft Press editorial team: Sally Stickney, project editor and manuscript editor; Devon Musgrave, manuscript editor; Jim Fuchs, technical editing consultant; Carl Diltz and Katherine Erickson, compositors; Joel Panchot, artist; and Holly M. Viola, copy editor. • Members of the Microsoft .NET Framework team: Fred Aaron, Brad Abrams, Mark Anders, Chris Anderson, Dennis Angeline, Keith Ballinger, Sanjay Bhansali, Mark Boulter, Christopher Brown, Chris Brumme, Kathleen Carey, Ian Carmichael, Rajesh Chandrashekaran, Yann Christensen, Suzanne Cook, Krzysztof Cwalina, Shajan Dasan, • 2 Peter de Jong, Blair Dillaway, Patrick Dussud, Erick Ellis Bill Evans, Michael Fanning, Greg Fee, Kit George, Peter Golde, Will Greg, Bret Grinslade, Brian Grunkemeyer, Eric Gunnerson, Simon Hall, Jennifer Hamilton, Brian Harry, Michael Harsh, Jonathan Hawkins, Anders Hejlsberg, Jim Hogg, Paul Johns, Gopal Kakivaya, Sonja Keserovic, Abhi Khune, Loren Kornfelder, Nikhil Kothari, Tim Kurtzman, Brian LaMacchia, Sebastian Lange, Serge Lidin, Francois Liger, Yung−Shin “Bala” Lin, Mike Magruder, Rudi Martin, Erik Meijer, Gene Milener, Jim Miller, Anthony Moore, Vance Morrison, David Mortenson, Yuval Neeman, Lance Olson, Srivatsan Parthasarathy, Mahesh Prakriya, Steven Pratchner, Susan Radke−Sproul, Jayanth Rajan, Dmitry Robsman, Jay Roxe, Dario Russi, Craig Schertz, Alan Shi, Craig Sinclair, Greg Singleton, Ralph Squillace, Paul Stafford, Larry Sullivan, Dan Takacs, Ryley Taketa, David Treadwell, Sean Trowbridge, Nate Walker, Sara Williams, Jason Zander, and Eric Zinda. If I’ve forgotten anyone, please forgive me. Reviewers: Keith Ballinger, Tom Barclay, Lars Bergstrom, Stephen Butler, Jeffrey Cooperstein, Robert Corstanje, Tarek Dawoud, Sylvain Dechatre, Ash Dhanesha, Shawn Elliott, Chris Falter; Lakshan Fernando, Manish Godse, Eric Gunnerson, Brian Harry, Chris Hockett, Dekel Israeli, Paul Johns, Jeanine Johnson, Jim Kieley, Alex Lerner, Richard Loba, Kerry Loynd, Rob Macdonald, Darrin Massena, John Noss, Piet Obermeyer, Peter Plamondon, Keith Pleas, Mahesh Prakriya, Doug Purdy, Kent Sharkey, Alan Shi, Dan Vallejo, Scott Wadsworth, Beth Wood, and Steven Wort. • Wintellectuals: Jim Bail, Francesco Balena, Doug Boling, Jason Clark, Paula Daniels, Dino Esposito, Lewis Frazer, John Lam, Jeff Prosise, John Robbins, Kenn Scribner, and Chris Shelby. • 3 [...]... send them to Microsoft Press using either of the following methods: Postal Mail: Microsoft Press Attn: Applied Microsoft NET Framework Programming Editor One Microsoft Way Redmond, WA 98052−6399 E−Mail: 13 MSPINPUT @MICROSOFT. COM Please note that product support is not offered through the above mail addresses For support information regarding C#, Visual Studio, or the NET Framework, visit the Microsoft. .. of the NET initiative, Microsoft is providing several products that companies can choose to use if their business logic (services) find them useful Here are some of Microsoft s enterprise server products: • Microsoft Application Center 2000 7 • Microsoft BizTalk Server 2000 • Microsoft Commerce Server 2000 • Microsoft Exchange 2000 • Microsoft Host Integration Server 2000 • Microsoft Internet Security... programming concepts So I decided to use C# as my programming language of choice throughout this entire book I chose C# because it is the language Microsoft designed specifically for developing code for the NET Framework If you’ve decided not to use C# for your programming projects, that’s OK—I’ll just assume that you can read C# even if you’re not 12 programming in it System Requirements The NET Framework. .. Although I don’t intend to teach basic programming, I will spend time on various programming topics that are specific to the NET Framework All NET Framework developers must be aware of these topics, which I explain and use throughout this book Finally, because this is a book about the NET Framework s common language runtime, it’s not about programming in any one specific programming language However, I provide... Web site at: http://support .microsoft. com 14 Part I: Basics of the Microsoft NET Framework Chapter List Chapter 1: The Architecture of the NET framework Development Platform Chapter 2: Building, Packaging, Deploying, and Administering Applications and Types Chapter 3: Shared Assemblies 15 Chapter 1: The Architecture of the NET Framework Development Platform The Microsoft NET Framework introduces many... (ISA) Server 2000 • Microsoft Mobile Information Server 2002 • Microsoft SQL Server 2000 It’s likely that each of these products will eventually have a .NET added to its name for marketing purposes But I’m also sure that over time, these products will integrate more NET features into them as Microsoft continues the initiative Microsoft XML Web Services: NET My Services Certainly, Microsoft wants to... known as Microsoft s .NET My Services.” You can find out more information about them at http://www .Microsoft. com/MyServices/ Over time, Microsoft will add many more consumer services and will also be creating business−oriented XML Web services In addition to these public Web services, Microsoft will create internal services for sales data and billing These internal services will be accessible to Microsoft. .. The existence of this file tells you that the NET Framework is installed However, several versions of the NET Framework can be installed on a single machine simultaneously If you want to determine exactly which versions of the NET Framework are installed, examine the subkeys under the following registry key: HKEY_LOCAL_MACHINE\SOFTWARE \Microsoft\ .NETFramework\policy When you build an EXE assembly, the... 6 What Makes Up the Microsoft NET Initiative I’ve been working with Microsoft and its technologies for many years now Over the years, I’ve seen Microsoft introduce all kinds of new technologies and initiatives: MS−DOS, Windows, Windows CE, OLE, COM, ActiveX, COM+, Windows DNA, and so on When I first started hearing about Microsoft s NET initiative, I was surprised at how solid Microsoft s story seemed... application types The NET Framework consists of two parts: the common language runtime (CLR) and the Framework Class Library (FCL) The NET Framework is the part of the initiative that makes developing services and applications really easy And, most important, this is what this book is all about: developing applications and XML Web services for the NET Framework Initially, Microsoft will make the CLR . Applied Microsoft .NET Framework Programming Table of Contents Applied Microsoft .NET Framework Programming. 1. Jeffrey. Applied Microsoft .NET Framework Programming / Jeffrey Richter. p. cm. Includes index. ISBN 0−7356−1422−9 1. Microsoft .NET Framework. 2. Internet programming.

Ngày đăng: 21/12/2013, 04:17

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan