PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2010 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 Control Number: 2009943026
Printed and bound in the United States of America.
1 2 3 4 5 6 7 8 9 WCT 5 4 3 2 1 0
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 infor mation about
international editions, contact your local Microsoft Corporation ofce or contact Microsoft Press International directly at
fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments to msinput@microsoft.com.
Microsoft, Microsoft Press, Active Accessibility, Active Directory, ActiveX, Authenticode, DirectX, Excel, IntelliSense,
Internet Explorer, MSDN, Outlook, SideShow, Silverlight, SQL Server, Visual Basic, Visual Studio, Win32, Windows,
Windows Live, Windows Media, Windows NT, Windows Server and Windows Vista are either registered trademarks
or trademarks of the Microsoft group of companies. 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 ctitious. No association with any real company, organization, product, domain name, e-mail address,
logo, person, place, or event is intended or should be inferred.
This book expresses the author’s views and opinions. The information contained in this book is provided without any
express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will
be held liable for any damages caused or alleged to be caused either directly or indirectly by this book.
Acquisitions Editor: Ben Ryan
Developmental Editor: Devon Musgrave
Project Editor: Valerie Woolley
Editorial Production: Custom Editorial Productions, Inc.
Technical Reviewer: Christophe Nasarre; Technical Review services provided by Content Master, a member of CM
Group, Ltd.
Cover: Tom Draper Design
Body Part No. X16-61995
iii
Table of Contents
Foreward ..........................................................xiii
Introduction .......................................................xv
Part I CLR Basics
1 The CLR’s Execution Model .....................................1
Compiling Source Code into Managed Modules ..................................1
Combining Managed Modules into Assemblies ...................................5
Loading the Common Language Runtime........................................6
Executing Your Assembly’s Code ................................................9
IL and Verification ......................................................15
Unsafe Code...........................................................16
The Native Code Generator Tool: NGen.exe .....................................18
The Framework Class Library ..................................................20
The Common Type System ....................................................22
The Common Language Specification ..........................................25
Interoperability with Unmanaged Code.........................................29
2 Building, Packaging, Deploying, and Administering Applications
and Types..................................................31
.NET Framework Deployment Goals ............................................32
Building Types into a Module..................................................33
Response Files .........................................................34
A Brief Look at Metadata .....................................................36
Combining Modules to Form an Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Adding Assemblies to a Project by Using the Visual Studio IDE...............49
Using the Assembly Linker...............................................50
Adding Resource Files to an Assembly ....................................52
Assembly Version Resource Information ........................................53
Version Numbers.......................................................57
Culture .....................................................................58
Simple Application Deployment (Privately Deployed Assemblies) ..................59
Simple Administrative Control (Configuration) ...................................61
Microsoft is interested in hearing your feedback so we can continually improve our books and learning
resources for you. To participate in a brief online survey, please visit:
www.microsoft.com/learning/booksurvey/
What do you think of this book? We want to hear from you!
iv Table of Contents
3 Shared Assemblies and Strongly Named Assemblies ..............65
Two Kinds of Assemblies, Two Kinds of Deployment..............................66
Giving an Assembly a Strong Name ............................................67
The Global Assembly Cache ...................................................73
Building an Assembly That References a Strongly Named Assembly ................75
Strongly Named Assemblies Are Tamper-Resistant ...............................76
Delayed Signing .............................................................77
Privately Deploying Strongly Named Assemblies.................................80
How the Runtime Resolves Type References .....................................81
Advanced Administrative Control (Configuration) ................................84
Publisher Policy Control.................................................87
Part II Designing Types
4 Type Fundamentals ...........................................91
All Types Are Derived from System.Object ....................................91
Casting Between Types .......................................................93
Casting with the C# is and as Operators .................................95
Namespaces and Assemblies ..................................................97
How Things Relate at Runtime ................................................102
5 Primitive, Reference, and Value Types ..........................113
Programming Language Primitive Types .......................................113
Checked and Unchecked Primitive Type Operations .......................117
Reference Types and Value Types .............................................121
Boxing and Unboxing Value Types ............................................127
Changing Fields in a Boxed Value Type by Using Interfaces (and Why You
Shouldn’t Do This) ...................................................140
Object Equality and Identity ............................................143
Object Hash Codes..........................................................146
The dynamic Primitive Type..................................................148
6 Type and Member Basics .....................................155
The Different Kinds of Type Members .........................................155
Type Visibility...............................................................158
Friend Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159
Member Accessibility ........................................................160
Static Classes ...............................................................162
Partial Classes, Structures, and Interfaces ......................................164
Components, Polymorphism, and Versioning...................................165
How the CLR Calls Virtual Methods, Properties, and Events.................167
Using Type Visibility and Member Accessibility Intelligently ................172
Dealing with Virtual Methods When Versioning Types . . . . . . . . . . . . . . . . . . . . .175
7 Constants and Fields .........................................181
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181
Fields ......................................................................183
Table of Contents v
8 Methods ...................................................187
Instance Constructors and Classes (Reference Types) ............................187
Instance Constructors and Structures (Value Types) .............................191
Type Constructors...........................................................194
Type Constructor Performance..........................................198
Operator Overload Methods .................................................200
Operators and Programming Language Interoperability ...................203
Conversion Operator Methods................................................204
Extension Methods..........................................................207
Rules and Guidelines...................................................210
Extending Various Types with Extension Methods .........................211
The Extension Attribute ................................................213
Partial Methods.............................................................213
Rules and Guidelines...................................................216
9 Parameters .................................................219
Optional and Named Parameters .............................................219
Rules and Guidelines...................................................220
The DefaultParameterValue and Optional Attributes ..................222
Implicitly Typed Local Variables ...............................................223
Passing Parameters by Reference to a Method..................................225
Passing a Variable Number of Arguments to a Method ..........................231
Parameter and Return Type Guidelines ........................................233
Const-ness.................................................................235
10 Properties ..................................................237
Parameterless Properties.....................................................237
Automatically Implemented Properties ..................................241
Defining Properties Intelligently.........................................242
Object and Collection Initializers ........................................245
Anonymous Types.....................................................247
The System.Tuple Type...............................................250
Parameterful Properties .....................................................252
The Performance of Calling Property Accessor Methods .........................257
Property Accessor Accessibility ...............................................258
Generic Property Accessor Methods...........................................258
11 Events .....................................................259
Designing a Type That Exposes an Event .......................................260
Step #1: Define a type that will hold any additional information
that should be sent to receivers of the event notification .................261
Step #2: Define the event member ......................................262
Step #3: Define a method responsible for raising the event to
notify registered objects that the event has occurred ....................263
Step #4: Define a method that translates the input into the desired event ....266
How the Compiler Implements an Event .......................................266
评论11
最新资源