Introduction

Jim Miller, Program Manager for the Common Language Runtime (CLR) at Microsoft, when interviewed by Robert Hess, defined the CLR as follows in The .NET Show (http://msdn.microsoft.com/theshow/Episode020/Transcripttext.asp):

Robert Hess: So what exactly is the CLR? If I want to talk [about] it as an object, what would you define it as?

Jim Miller: Well, it is hard to define as a single thing. It is a combination of things. It is a file format that is used for transferring files between systems that are using the .NET framework, essentially. It is the programming goo inside there that lets your program run. It is the thing that takes the way your program has been compiled into this Intermediate Language, and actually turns that into machine code and… executes it, plus all the support services that you need while it's running… Memory management services, exception handling services, compilation services, reflection services—[the CLR] encompasses all of those pieces, but just up the sort of the very lowest level to bring you up to essentially the level you would have been at, let's say, if you were doing programming in C with just the minimal runtime, no libraries above it. The libraries above it are part of what we call the framework, and that's sort of the next level of the system.

Whether you are running code derived from VB, C#, managed C++, JScript, or any of the other 15–20 languages supported by the .NET Framework, you are using the facilities and services of the CLR. The CLR provides services that promise to move software development into the 21st century. The CLR provides a whole new way of developing software. This book provides you with the information and samples you need to quickly apply and use the CLR to its fullest potential.

This Book's Intended Audience

This book is primarily targeted toward software engineers who require a thorough understanding of the underpinnings of the .NET Framework. It is intended to assist those engineers who understand the vision of a managed environment and believe that it is one of the primary means by which software productivity will rise to the next level. It is targeted toward those engineers who believe that although the .NET Framework frees the programmer from many mundane tasks, it does not replace the need for true understanding of the platform and tools upon which he or she is developing software.

What You Need to Know Prior to Reading This Book

This book makes many assumptions about the experience you need prior to reading this book. In some chapters, this is more true than others. If you have experience programming in threads, you will better appreciate the chapter on threading. If you have experience with peer-to-peer networking, you will better appreciate the chapter on networking. If you have experience with trying to lock down your system and security, you will better appreciate the chapter on security. If you have attempted to make your application global, you will better appreciate the chapter on globalization and localization. All readers who have experience developing software undoubtedly have experience debugging and profiling it. In this case, you certainly will appreciate the chapters on profiling and debugging. Finally, if you have experience in developing Java code, you will certainly be able to appreciate Appendix D,“The Common Language Runtime as Compared to the Java Virtual Machine.” You will be able to appreciate the examples and the principles of the Common Language Runtime more if you have experience in developing traditional or unmanaged code. If you find that you need to understand the CLR and what it can do for your company and you are a little short on experience but are well motivated, the concepts presented should not be overwhelming.

Appendix A contains a brief tutorial on C#. If you cannot read and understand C#, then much of the sample code will not be clear. If the overview in Appendix A is insufficient for your understanding, you should seek some familiarity with C# before reading this book.

Scattered throughout this book are samples from other languages, such as VB, C++, JScript, Java, Perl, J#, and so forth. Usually, these samples are presented in the context of comparing another language's implementation with a C# implementation. If you do not understand all of the syntax of the language, try to see a pattern between that language and C#.

What You Will Learn from This Book

There is so much to learn and grasp in this book that it might seem overwhelming. Read the book once cover to cover and then refer back to it often. Download the samples for each chapter, formulate theories about different runtime scenarios that are not specifically covered by the sample, modify the sample, and try it. In other words, use this book as a starting point in your quest for understanding. If you have an immediate need for understanding in one subject or another, don't hesitate to jump to that chapter and study it and the samples thoroughly. Keep in mind, however, that some background material might be required to understand a particular chapter out of context. If this is the case, refer to the appropriate chapter. If you approach the book in this manner, you will gain the following:

  • Understanding of .NET Types and the Common Type System (CTS)

  • .NET Assembly metadata structure and layout

  • COM/COM+ interoperation with .NET components

  • Legacy integration with Win32 DLL's through P/Invoke

  • CLR memory/resource management

  • Management and use of threads in a .NET environment

  • Ability to build high-performance peer-to-peer networking applications

  • Use of remoting for next generation distributed computing

  • Flexible application interaction with events and delegates

  • Integration of .NET error handling into your application with exceptions

  • Building and maintaining a secure application with .NET security

  • Dynamic discovery of type information through .NET reflection

  • Targeting of an international audience using .NET globalization/localization tools

  • Debugging of a .NET application

  • Profiling of a .NET application

  • Overview of key C# syntax and design issues

  • Overview of .NET framework libraries

  • Hosting of your own CLR

  • A comparison of the CLR and JVM

Software Needed to Complete the Examples Provided with This Book

Most of the samples in this book require the latest version of the .NET Framework SDK. For most of the samples in the book, it is assumed that Visual Studio .NET has also been successfully installed. Appendix D requires the installation of the latest version of the Java SDK from Sun Microsystems. The appropriate URL to a download site is provided in the text. A couple of small examples are written in Perl and a few more are written in J#. Installing these languages is certainly required to compile and run the samples. It is also possible to skip actually building these samples and just glean what you can from the source. Doing so will not detract too much from your overall understanding of the CLR and the concepts presented.

How This Book Is Organized

This book is divided into four parts. Part I establishes the architecture around the CLR and the operating environment that the CLR creates for your application. Part II introduces the assembly and how it is organized and used in the runtime environment. This part also discusses the assembly as a standard unit of deployment within the .Net Framework. Part III discusses various services that the CLR offers an application and how best to take advantage of those services. Part IV contains five Appendixes that offer supplemental information you might find useful.

  • Part I: .NET Framework and the CLR Fundamentals— The chapters that make up Part I describe the .NET Architecture. This part describes the Common Type System (CTS) and the Common Language System (CLS) that allow multiple high-level languages to efficiently and completely interoperate with each other.

    • Chapter 1—This chapter introduces the idea of managed code and some of the benefits that are available from managed code—in particular, the CLR.

    • Chapter 2—This chapter describes the types that make up the .NET Framework and how the CLR manipulates these types and your application.

    • Chapter 3—This chapter presents an overview of the runtime environment in which your .NET application runs. It provides a step-by-step overview of how an assembly is loaded in preparation to be executed.

  • Part II: Components of the CLR— The Assembly can be thought of as self-describing code. The data that describes the data is known as metadata. Part II describes the metadata in an assembly and how it is organized. It also has a description of the Intermediate Language (IL) that is part of every assembly. Finally, this part describes how to install an assembly.

    • Chapter 4—This chapter focuses not only on the types of metadata that are contained in an assembly, but also provides two unmanaged methods for accessing the metadata within an assembly.

    • Chapter 5—This chapter provides enough information about the various opcodes that comprise IL to make you at least IL literate. You will undoubtedly be constantly referring to the IL code that is generated by the high-level language compiler of your choice.

    • Chapter 6—This chapter focuses on tools and ensure that what is built and tested is what is finally delivered to the customer. Nothing is more frustrating than having code that you have thoroughly debugged blow up at a customer's site.

  • Part III: Runtime Services Provided by the CLR— The remainder of the book describes each of the services that the CLR offers. Along with the description are examples of how you can take full advantage of these services to make your application more portable, more secure, and more maintainable.

    • Chapter 7—Win32 has been around for quite some time now. There is such a large body of code that has been developed that some means was required from within a .NET component to access these legacy libraries and APIs. The .NET facility to do this is platform invoke, or P/Invoke.

    • Chapter 8—Another type of software that is at least as ubiquitous as Win32 DLLs is COM components. To ensure that traditional COM components do not need to be ported or simply thrown away, the .NET Framework provides a method to import the type library information from a COM type library and build a wrapper around the component.

    • Chapter 9—This chapter shows you how to make a .NET component usable from within unmanaged code as a COM component.

    • Chapter 10—This chapter shows how the CLR manages memory allocation and deallocation using an efficient garbage collection algorithm. This chapter also discusses how you can most efficiently manage resources that are not memory related, as well as hooks that are available for you to step in and modify the management of resources.

    • Chapter 11—This chapter focuses on the facilities that are available for manipulating and using threads. Threads are abstracted by the .NET Framework into a logical thread that is encapsulated by the Thread class.

    • Chapter 12—This chapter illustrates how you can use networking support in your application to develop a fast and efficient peer-to-peer network solution. Networking as presented in the System.Net namespace is not a service directly provided by the CLR; however, networking is at the base of most of the .NET Framework and certainly key in XML Web Services and Remoting.

    • Chapter 13—This chapter seeks to remove much of the mystery behind remoting so that you can fully use the remoting services in your application. Because a remoting application is so ingrained into much of the .NET Framework and because much of its functionality is so automatic, it is often described as “magic happens here.”

    • Chapter 14—This chapter details how you can use and extend the event model of the .NET Framework in your application. Windows has long been an event-driven environment. The CLR brings the event model into the core of the runtime with events and delegates.

    • Chapter 15—This chapter details what exceptions are and how they can be used and extended. One of the main drawbacks of the Win32 APIs and with programming in general is the lack of a cohesive and uniform method for handling errors and exceptional conditions. The CLR again brings exception handling into the core of the operating environment.

    • Chapter 16—This chapter address some of the key security features that the CLR brings to the table, mainly under the headings of code access security and role access security. With a new debilitating virus being created virtually every day, security is important to all persons who are using or developing software.

    • Chapter 17—This chapter focuses on accessing the metadata from the runtime through reflection. Reflection allows for the discovery and creation of metadata at runtime as opposed to statically analyzing an assembly.

    • Chapter 18—This chapter focuses on how to use the information that is provided in the metadata to build a global and localized application. The CLR considers an application to be global from the start.

    • Chapter 19—This chapter details the tools that you will need to debug your .NET application. Because of .NET's unique runtime environment, debugging a .NET application requires some specialized tools.

    • Chapter 20—This chapter focuses on tools that are available to profile your .NET application so that you can fine-tune it. A .NET application uses memory and disk space much like any other application. However, a .NET application also supports JIT compilation, garbage collection, and security checks that are specific to a .NET application.

  • Part IV: Appendixes— The Appendixes are provided as background information for this book. Each Appendix supplies information that is considered important, but that is either out of the scope of this book or is not applicable to all readers.

    • Appendix A—This Appendix provides a brief overview of some C# constructs that will act as either a reminder or as a base level of understanding so that the samples are more readily understood. The samples are written in C# throughout this book.

    • Appendix B—This Appendix provides a broad overview of the libraries and classes that are at your disposal as part of the .NET Framework SDK.

    • Appendix C—This Appendix presents a discussion of how you can build your own host for the CLR.

    • Appendix D—This Appendix provides a brief comparison of the JVM and the CLR from a programmer's perspective.

    • Appendix E—This Appendix lists additional resources that are considered important to concepts developed in this book.

The Sams Web Site for This Book

The chapter-by-chapter code files that are described in this book are available on the Sams Web site at http://www.samspublishing.com/. Enter this book's ISBN (0672321246) in the Search box and click Search. When the book's title is displayed, click it to go to a page where you can download all the code. The code can be downloaded on a chapter-by-chapter basis.

Conventions Used in This Book

The following typographic conventions are used in this book:

  • Code lines, commands, statements, variables, file drives, programming functions, APIs, directories, and any text you type or see onscreen appears in a monospace typeface. Bold monospace typeface is used to designate classes and namespaces that are part of the .NET Framework class libraries.

  • Placeholders in syntax descriptions appear in an italic monospace typeface. Replace the placeholder with the actual filename, parameter, or whatever element it represents.

  • Italic is used to highlight technical terms when they are being defined.

  • The icon is used before a line of code that is really a continuation of the preceding line. If you see before a line of code, remember that it's part of the line immediately above it. This is not part of the code, just a book's convention.

  • This book also contains Notes, Tips, and Cautions to help you spot important or useful information more quickly. Some of these are helpful shortcuts to help you work more efficiently.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.62.45