Applied Microsoft NET Framework Programming

Over the years, our computing lifestyles have changed. Today, everyone sees the value of the Internet, and our computing lifestyle is becoming more and more dependent on Web−based services. Personally, I love to shop, get traffic conditions, compare products, buy tickets, and read product reviews all via the Internet. However, I’m finding that there are still many things I’d like to do using the Internet that aren’t possible today. For example, I’d like to find restaurants in my area that serve a particular cuisine. Furthermore, I’d like to be able to ask if the restaurant has any seating for, say, 7:00 p.m. that night. Or if I had my own business, I might like to know which vendor has a particular item in stock. If multiple vendors can supply me with the item, I’d like to be able to find out which vendor offers the least expensive price for the item or maybe which vendor can deliver the item to me the fastest. Services like these don’t exist today for two main reasons. The first reason is that no standards are in place for integrating all this information. After all, vendors today each have their own way of describing what they sell. The emerging standard for describing all types of information is Extensible Markup Language (XML). The second reason these services don’t exist today is the complexity of developing the code necessary to integrate such services. Microsoft has a vision in which selling services is the way of the future—that is, companies will offer services and interested users can consume these services. Many services will be free; others will be available through a subscription plan, and still others will be charged per use. You can think of these services as the execution of some business logic. Here are some examples of services: Validating a credit card purchase • Getting directions from point A to point B • Viewing a restaurant’s menu • Booking a flight on an airline, a hotel room, or a rental car • Updating photos in an online photo album • Merging your calendar and your children’s calendars to plan a family vacation • Paying a bill from a checking account • Tracking a package being shipped to you •

pdf441 trang | Chia sẻ: ttlbattu | Lượt xem: 5173 | Lượt tải: 4download
Bạn đang xem trước 20 trang tài liệu Applied Microsoft NET Framework Programming, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
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.............................................................................................