DotNet CLR Tutorial(good 多).doc_第1页
DotNet CLR Tutorial(good 多).doc_第2页
DotNet CLR Tutorial(good 多).doc_第3页
DotNet CLR Tutorial(good 多).doc_第4页
DotNet CLR Tutorial(good 多).doc_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/tt/articles/showarticle.tss?id=DM_CLR#otherApril 20, 2004 Overview CLR Basics History and Motivation Core Features and Services Other Features and Services Summary Resources PURPOSEThe purpose of this document is to introduce developers and other technical staff to the most important piece of Microsofts .NET strategy, the Common Language Runtime. This web site is intended to be a resource to the following audiences: Managers, developers, systems administrators and others with a technical background who want to understand what the CLR is, and how it fits into Microsofts .NET strategy After successfully completing this tutorial, you should be able to: Understand how the CLR fits into .NET Understand Microsofts reasons for developing the CLR Explain how the CLR overcomes the deficiencies of the technologies it replaces Understand some of the services that the CLR provides PREREQUISITESFamiliarity with basic programming concepts TOOLSYoull need the following tools to complete this tutorial: This is a non-programming tutorial, so no tools are required AUTHORSCraig Andera (principal author) The author would also like to thank the following for their contributions: Don Box, Keith Brown, Tim Ewald, Jon Flanders, Stu Halloway, Paul Kirby, Jose Mojica, Chris Sells, Dan Sullivan, Jason Whittington, Mike Woodring, Kim Wolk, Justin Gehtland, Julie Ryan. OVERVIEWMicrosofts .NET is a broad family of products representing the companys next generation of services, software, and development tools. At the core of the .NET strategy lives the Common Language Runtime. The CLR is a platform for software development that provides services by consuming metadata. It is standards-based and component-oriented. Like any platform, the important pieces are the runtime, the libraries that provide access to them, and the languages that can target the platform. The aim of this tutorial is to provide a foundation for forming and answering questions about the technical aspects of the CLR. We will examine this technology at a high level, defining and touching on each of the core aspects - runtime, libraries, and languages - in turn. Additionally, well look at the extensive support the CLR gives to standards-based and component-oriented software development. Well start with a broad overview of the CLR - what are the major pieces, what was the motivation for moving to this new model, and what benefits it provides. Then well dive down to cover some aspects of the CLR in greater depth. References to sources for further, more detailed study will be given throughout. Note that although every effort was made to ensure that the information contained within this document was accurate at the time of publication, the technologies mentioned here are subject to change. Caveat emptor! CLR BASICSThe Common Language Runtime is the core of Microsofts .NET vision. The .NET vision was officially introduced at the Microsoft Professional Developers conference in Orlando, Florida, in July 2000, although at the time much of the documentation referred to it as Next Generation Windows Services. Since the PDC, Microsoft has continued to expand upon the list of products and services associated with the .NET name. In keeping with their tradition of defining vague marketing terms (think ActiveX - did anyone ever figure out exactly what that meant?), the moniker .NET has been applied to everything from the next version of the Windows operating system to development tools. Its only half a joke to suggest that we will soon see Age of Empires.NET hit the shelves of computer gaming stores. This effort on Microsofts part to frame everything from mice to FoxPro in terms of .NET is actually a good sign: it indicates to consumers such as you and me that Microsoft is serious about the product, that it represents both a core part of their strategy, and that they are making a fundamental and massive shift. In the same way that they did with COM in the mid 1990s and with the Internet in later years, Microsoft is (in their own words) betting the company on this new technology. But what exactly is .NET? Although the precise meaning can be a little hard to isolate by reading the prolific marketing literature, a little digging reveals that .NET is in fact Microsofts grand strategy for how all of their software, systems, and services will fit together. It includes development tools (like the new version of Visual Studio, dubbed Visual Studio.NET), future versions of their Windows operating systems, new Internet-based services (like a stepped-up version of their Passport web authentication service), and an entirely new beast called the Common Language Runtime. The Common Language Runtime is the single most important piece of the .NET product strategy, because it is in essence the engine that pulls the train - the CLR is how developers will write software in the brave new .NET world (see figure 1). For that reason, this tutorial will focus on the CLR exclusively. For information about other .NET technologies, such as HailStorm (a set of web-enabled services based around Microsofts Passport technology), Visual Studio.NET, and the rest of Microsofts .NET vision, visit /net. Figure 1: .NET Architecture The CLR as a development platformThe CLR is a development platform. Like any platform, it provides a runtime, defines functionality in some libraries, and supports a set of programming languages. The CLR is a platform for developing applications. A platform is a set of programmatic services, exposed through some API to developers using one or more languages. Development generally targets a single platform; when I write a program using Visual Basic, I say that Im writing it for Windows, my target platform. The forms and controls that I develop wont run directly on, say, Apples Mac OS X. The CLR is not an operating system in the strict sense of the term - it does not, for example, provide a file system, relying instead on the underlying OS (such as Windows) to implement that feature. The CLR is, however, a platform, and in much the same way that code written for Unix will not run on Windows, code must specifically target the CLR. Dont panic, though, because theres plenty of consideration given to interoperating with existing, non-CLR code. Youll still be able to use your existing COM objects and DLLs while taking advantage of the new features of CLR development. The Common Language Runtime is Microsofts development platform of the future. In Microsofts vision of the world, most future software will be written to make use of the CLR features. Well be looking at what the CLR provides you so you can decide for yourself whether the advantages outweigh the costs. Now, assuming that you agree that this new platform offers significant advantages over your current platform, you might wonder, Whats the big picture? What things do I need to learn in order to develop for the CLR? When I approach any new platform, be it a new operating system, the CLR, or even an application suite that allows automation of its features, like Microsoft Office or SAS, I mentally break down the feature set of the platform into three fundamental areas: the runtime that the platform offers, the libraries it defines, and the languages Im going to use. These aspects of the platform overlap (see figure 2), and understanding each of them and the ways in which they interact is crucial to becoming an effective CLR programmer. Figure 2: The Fundamental Areas RuntimeA runtime provides services to software that you write. The CLR provides a runtime. Lets start with the first and arguably most important of the three fundamental areas in figure 2, the runtime. A runtime is a piece of code, written by the platform vendor, which provides your code with a set of services. What sorts of services? Well, it depends on the platform - it might be anything from checking security for you to implementing a file system to providing access to some piece of hardware. I like to think of a runtime as a sort of butler for my code. My code asks the runtime to do things, and the runtime goes off and does them. This is nice because it means I can concentrate on writing code that has to do with the problem Im trying to solve - like providing a system that approves customers for mortgages over the web - and not on writing a whole lot of low-level grungy code having to do with things like interacting with hardware. Almost every program written these days takes advantage of some sort of runtime. Very few programmers start a project by writing their own file system or database engine. Rather, they make use of already-written pieces of software, like Windows 2000 or Oracle. Both of these platforms (yep, theyre both platforms) have a runtime that provides services. In the case of Windows 2000, the runtime is the operating system kernel, and it provides services like thread management. In the case of Oracle, the runtime is the database engine, and the services include things like a SQL engine and transactions. We say that we write code that runs on a platform because it uses the services provided by the platform. The CLR provides these services using a layered architecture that is shown in figure 3. In some cases our code uses the CLR directly and in others indirectly. Figure 3: Layered Architecture of the CLR Some other examples of a runtime you may have used before are the Visual Basic Runtime, the SQL Server Engine, and the COM+/MTS runtime. Each of these provides services in a generic way so that you can rely on the runtime to do the work, rather than having to write the code yourself every time. The CLR is a runtime. In fact, thats what the R stands for. The documentation refers to this as the execution engine, but thats just another name for the same thing. Ill just call it the runtime or the CLR from here on out, and well know were talking about the CLRs execution engine. Well talk extensively about what services the runtime provides as we get into more detail later on. The job of the CLR is to watch over your code, taking care of its needs and wants. In fact, code that takes advantage of the CLR is said to be managed code, because the runtime is taking care of it, making sure that it has everything it needs and ensuring that it doesnt do anything its not supposed to. Unmanaged code is the code that either doesnt know about or doesnt want to use the runtime. Both types of code are shown in figure 3. In figure 3, we can see the CLR1 sitting just below our code. Note that the CLR requires the presence of some operating system - it is not an operating system itself. Today that means Windows 2000, Windows NT4, Windows 98, Windows ME, or Windows XP must be installed on the machine where you want to use the CLR, but eventually support for other operating systems may be developed2. Note also that unmanaged and managed code can coexist on the same system, and can communicate with each other. This is important to preserve your investment in any code you have today that doesnt use the CLR. Well talk in more detail about this later on. LibrariesThe CLRs Base Class Library allow us to interact with the runtime, and provide additional useful functionality. In order for my code to take full advantage of the runtime, I need a way to interact with the CLR. If I were programming unmanaged code where the operating system was the one providing the services I needed (e.g. drawing windows as part of a user interface), I would use a set of functions provided for me in the operating systems API, or Application Programming Interface. These are represented by the vertical arrows shown in figure 3. The CLR Software Development Kit (SDK) provides an API. Because the CLR favors object-oriented programming (although other types of programming are supported), this API takes the form of a set of classes. Collectively, they are referred to the Base Class Library, or BCL. Through the classes in the BCL3, we can interact with the runtime, influencing the way that the runtimes services are provided to us. That, however, is not all that the BCL gives us. In addition to giving us an in to the runtime, the BCL classes provide a large number of useful utilities. These include things like a new database access library, ASP.NET, and an XML parser with support for the latest XML specifications. There are literally thousands of classes in the BCL, so a complete listing is beyond the scope of this whitepaper. Well come back and look in more detail at some of the functionality provided by some of the BCL in the second half of this whitepaper. For complete information, check the out the documentation at /library under the heading .NET Development.its actually quite good! LanguagesThe CLR supports programming in one of about two dozen languages. The most popular of these are likely to be C# and Visual Basic.NET. Having a set of libraries and a runtime is great, but neither one of them does me any good if I cant write programs to take advantage of them. In order to do that, we need to use some programming language with a compiler that is runtime-aware. Microsoft currently lists over twenty different languages with which it will be possible to write software that targets the CLR4. Microsoft themselves ship support for five languages with the SDK: C#, Visual Basic.NET, IL, C+, and JScript.NET. Of these, C# and Visual Basic.NET are likely to be the languages most often used to develop software for this new platform. C# (pronounced see-sharp) is a new language with a C and C+ heritage. Developers familiar with either existing language or with Java will find the syntax very familiar. Several books about C# have already hit the shelves, including C# Essentials, by Albahari, Drayton, and Merril from OReilly & Associates5. Visual Basic.NET is an updated version of Microsofts most popular programming language. The syntax differs significantly from that of VB6, and support has been added for a raft of object-oriented mechanisms not available in VB6. Moving code from VB6 to VB.NET in order to take advantage of the CLR will in most cases be a significant porting effort, even with the automated help that Visual Studio.NET provides6 7. Having a runtime, providing libraries, and supporting a set of programming languages is something that all platforms do. What other things does the CLR enable that I cant easily do with straight-ahead, unmanaged Windows programming? In two areas, the CLR really shines: its support for component-based programming, and its extensive use of open, standards-based technologies. ComponentsThe CLR has extensive support for component-based programming. A fairly recent trend in software development is that of component-oriented programming, although the idea behind component development is not a new one. In fact, its not even unique to software engineering: we stole it from the hardware guys. The basic concept is that systems are built out of discrete parts that can be assembled to make a larger whole. This is similar to the way, for example, your CD player is composed of a number of computer chips wired together with a laser and an LCD display. The idea in software engineering is to leverage this same pattern. Software systems can be built from software components. A software component is a discrete piece of functionality that can be plugged into different applications. For example, I might develop a calendar component that allows the user to pick a day of the month. It could then be used in a Visual Basic application, on a web page, or in a Microsoft Word document. Of course, components do not have to be visual - a reusable piece of logic for calculating sales tax could also be written as a component. The important idea is that theyre independent pieces that can be plugged together in a large system. Component development has numerous attractive benefits. For one, the ability to assemble existing parts into a system offers the hope of true code reuse, a long sought-after goal. And even if you write all your own code from scratch, organizing it as components can ease maintenance by allowing independent bug fixes to be deployed. The CLR is, from the ground up, a component-oriented platform. Every piece of CLR functionality that we run must belong to a component - an assembly in CLR parlance. Further, this component-orientation is deeply rooted in the mechanisms of the CLR, to the point where the security subsystem and the loader (among others) have the notion of component baked in. StandardsThe CLR supports several standards, such as XML and SOAP. The CLR has itself been submitted as an open standard.Although the heady days of buying stock solely based on the presence of the letter e or the suffix .com have come and gone, theres no question that the Internet offers new opportunities for businesses. Systems that take advantage of what a global communication network can offer are here to stay. Without standards, however, this would not be possible. A standard is a document that describes a convention or protocol that everyone agrees to follow. One example of a standard is HTTP - the Hypertext Transfer Protocol. Without HTTP, you wouldnt be able to walk up to any website with any browser and view the contents. Imagine if I could only view Linux websites with a Netscape browser running on the same version of Linux. It would severely limit the utility of the web. Because we have standards like HTTP, broad reach is possible. Support for standards is of key importance to developers creating software today. The CLR recognizes this, and provides explicit support for standards-ba

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论