




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Advantages of Managed CodeMicrosoft intermediate language shares with Java byte code the idea that it is a low-level language with a simple syntax , which can be very quickly translated into native machine code. Having this well-defined universal syntax for code has significant advantages.Platform independenceFirst, it means that the same file containing byte code instructions can be placed on any platform; at runtime the final stage of compilation can then be easily accomplished so that the code will run on that particular platform. In other words, by compiling to IL we obtain platform independence for .NET, in much the same way as compiling to Java byte code gives Java platform independence.Performance improvementIL is actually a bit more ambitious than Java byte code. IL is always Just-In-Time compiled (known as JIT), whereas Java byte code was often interpreted. One of the disadvantages of Java was that, on execution, the process of translating from Java byte code to native executable resulted in a loss of performance.Instead of compiling the entire application in one go (which could lead to a slow start-up time), the JIT compiler simply compiles each portion of code as it is called (just-in-time). When code has been compiled.once, the resultant native executable is stored until the application exits, so that it does not need to be recompiled the next time that portion of code is run. Microsoft argues that this process is more efficient than compiling the entire application code at the start, because of the likelihood that large portions of any application code will not actually be executed in any given run. Using the JIT compiler, such code will never be compiled.This explains why we can expect that execution of managed IL code will be almost as fast as executingnative machine code. What it doesnt explain is why Microsoft expects that we will get a performanceimprovement. The reason given for this is that, since the final stage of compilation takes place at runtime, the JIT compiler will know exactly what processor type the program will run on. This means that it can optimize the final executable code to take advantage of any features or particular machine code instructions offered by that particular processor.实际上,IL比Java字节代码的作用还要大。IL总是即时编译的(简称JIT),而Java字节代码常常是解释型的,Java的一个缺点是,在运行应用程序时,把Java字节代码转换为内部可执行代码的过程可可能导致性能的损失。JIT编译器并不是把整个应用程序一次编译完(这样会有很长的启动时间),而是只编译它调用的那部分代码。代码编译过一次后,得到的内部可执行代码就存储起来,直到退出该应用程序为止,这样在下次运行这部分代码时,就不需要重新编译了。Microsoft认为这个过程要比一开始就编译整个应用程序代码的效率高得多,因为任何应用程序的大部分代码实际上并不是在每次运行过程中都执行。使用JIT编译器,从来都不会编译这种代码从来都不会被编译。这解释了为什么托管IL代码的执行几乎和内部机器代码的执行速度一样快,但是并没有说明为什么Microsoft认为这会提高性能。其原因是编译过程的最后一部分是在运行时进行的,JIT编译器确切地知道程序运行在什么类型的处理器上,利用该处理器提供的任何特性或特定的机器代码指令来优化最后的可执行代码。传统的编译器会优化代码,但它们的优化过程是独立于代码所运行的特定处理器的。这是因为传统的编译器是在发布软件之前编译为内部机器可执行的代码。即编译器不知道代码所运行的处理器类型,例如该处理器是x86兼容处理器或Alpha处理器,这超出了基本操作的范围。例如Visual Studio 6优化了一台一般的Pentium机器,所以它生成的代码就不能利用Pentium III处理器的硬件特性。相反,JIT编译器不仅可以进行Visual Studio 6所能完成的优化工作,还可以优化代码所运行的特定处理器。Traditional compilers will optimize the code, but they can only perform optimizations that are independent of the particular processor that the code will run on. This is because traditional compilers compile to native executable before the software is shipped. This means that the compiler doesnt know what type of processor the code will run on beyond basic generalities, such as that it will be an x86-compatible processor or an Alpha processor. Visual Studio 6, for example, optimizes for a generic Pentium machine,so the code that it generates cannot take advantage of hardware features of Pentium III processors. Onthe other hand, the JIT compiler can do all the optimizations that Visual Studio 6 can, and in addition itwill optimize for the particular processor the code is running on.Language interoperabilityThe use of IL not only enables platform independence; it also facilitates language interoperability. Simply put, you can compile to IL from one language, and this compiled code should then be interoperable with code that has been compiled to IL from another language.Youre probably now wondering which languages aside from C# are interoperable with .NET, so letsbriefly discuss how some of the other common languages fit into .NET.Visual Basic .NETVisual Basic .NET has undergone a complete revamp from Visual Basic 6 to bring it up-to-date with .NET. The way that Visual Basic has evolved over the last few years means that in its previous version, Visual Basic 6, it was not a suitable language for running .NET programs. For example, it is heavily integrated into COM and works by exposing only event handlers as source code to the developermost of the background code is not available as source code. Not only that, it does not support implementation inheritance, and the standard data types Visual Basic 6 uses are incompatible with .NET. Visual Basic 6 was upgraded to Visual Basic .NET, and the changes that were made to the language are so extensive you might as well regard Visual Basic .NET as a new language. Existing Visual Basic 6 code does not compile as Visual Basic .NET code. Converting a Visual Basic 6 program to Visual Basic .NET requires extensive changes to the code. However, Visual Studio .NET (the upgrade of VS for use with .NET) can do most of the changes for you. If you attempt to read a Visual Basic 6 project into Visual Studio .NET, it will upgrade the project for you, which means that it will rewrite the Visual Basic 6 source code into Visual Basic .NET source code. Although this means that the work involved for you is heavily cut down, you will need to check through the new Visual Basic .NET code to make sure that the project still works as intended because the conversion might not be perfect.One side effect of this language upgrade is that it is no longer possible to compile Visual Basic .NET to native executable code. Visual Basic .NET compiles only to IL, just as C# does. If you need to continue coding in Visual Basic 6, you may do so, but the executable code produced will completely ignore the .NET Framework, and youll need to keep Visual Studio 6 installed if you want to continue to work in this developer environment.Visual C+ .NETVisual C+ 6 already had a large number of Microsoft-specific extensions on Windows. With Visual C+ .NET, extensions have been added to support the .NET Framework. This means that existing C+ source code will continue to compile to native executable code without modification. It also means, however, that it will run independently of the .NET runtime. If you want your C+ code to run within the .NET Framework, then you can simply add the following line to the beginning of your code:#using You can also pass the flag /clr to the compiler, which then assumes that you want to compile to managedcode, and will hence emit IL instead of native machine code. The interesting thing about C+ isthat when you compile to managed code, the compiler can emit IL that contains an embedded nativeexecutable. This means that you can mix managed types and unmanaged types in your C+ code. Thusthe managed C+ code:class MyClassdefines a plain C+ class, whereas the code:_gc class MyClasswill give you a managed class, just as if youd written the class in C# or Visual Basic .NET. The advantageof using managed C+ over C# code is that we can call unmanaged C+ classes from managed C+code without having to resort to COM interop.The compiler raises an error if you attempt to use features that are not supported by .NET on managedtypes (for example, templates or multiple inheritance of classes). You will also find that you will need touse nonstandard C+ features (such as the _gc keyword shown in the previous code) when usingmanaged classes.Because of the freedom that C+ allows in terms of low-level pointer manipulation and so on, the C+compiler is not able to generate code that will pass the CLRs memory type safety tests. If its importantthat your code is recognized by the CLR as memory type safe, then youll need to write your source codein some other language (such as C# or Visual Basic .NET).Visual J# .NETThe latest language to be added to the mix is Visual J# .NET. Prior to .NET Framework 1.1, users wereable to use J# only after making a separate download. Now the J# language is built into the .NETFramework. Because of this, J# users are able to take advantage of all the usual features of Visual Studio.NET. Microsoft expects that most J+ users will find it easiest to use J# if they want to work with .NET.Instead of being targeted at the Java runtime libraries, J# uses the same base class libraries that the restof the .NET compliant languages use. This means that you can use J# for building ASP.NET Web applications,Windows Forms, XMLWeb services, and everything else that is possiblejust as C# and VisualBasic .NET can.Scripting languagesScripting languages are still around, although, in general, their importance is likely to decline with theadvent of .NET. JScript, on the other hand, has been upgraded to JScript .NET. We can now write ASP.NETpages in JScript .NET, run JScript .NET as a compiled rather than an interpreted language, and writestrongly typed JScript .NET code. With ASP.NET there is no reason to use scripting languages in serversideWeb pages. VBA is, however, still used as a language for Microsoft Office and Visual Studio macros.COM and COM+Technically speaking, COM and COM+ arent technologies targeted at .NET, because components basedon them cannot be compiled into IL (although its possible to do so to some degree using managed C+, ifthe original COM component was written in C+). However, COM+ remains an important tool, becauseits features are not duplicated in .NET. Also, COM components will still workand .NET incorporatesCOM interoperability features that make it possible for managed code to call up COM components andvice versa (this is discussed in Chapter 29). In general, however, you will probably find it more convenientfor most purposes to code new components as .NET components, so that you can take advantage of the.NET base classes as well as the other benefits of running as managed code.托管代码的优点Microsoft中间语言与Java字节代码共享一种理念:它们都是一种低级语言,语法很简单,可以非常快速地转换为机器码。对于代码来说,这种精心设计的通用语法,有很大的优点。1. 平台无关性首先,这意味着包含字节代码指令的同一个文件可以放在任一个平台中,运行时编译过程的最后阶段可以很容易完成,这样代码就可以运行在该特定的平台上。也就是说编译为中间语言就可以获得.NET平台无关性,这与编译为Java字节代码就会得到Java平台无关性是一样的。2. 提高性能实际上,IL比Java字节代码的作用还要大。IL总是即时编译的(简称JIT),而Java字节代码常常是解释型的,Java的一个缺点是,在运行应用程序时,把Java字节代码转换为内部可执行代码的过程可可能导致性能的损失。JIT编译器并不是把整个应用程序一次编译完(这样会有很长的启动时间),而是只编译它调用的那部分代码(这是其名称由来)。代码编译过一次后,得到的内部可执行代码就存储起来,直到退出该应用程序为止,这样在下次运行这部分代码时,就不需要重新编译了。Microsoft认为这个过程要比一开始就编译整个应用程序代码的效率高得多,因为任何应用程序的大部分代码实际上并不是在每次运行过程中都执行。使用JIT编译器,从来都不会编译这种代码。这解释了为什么托管IL代码的执行几乎和内部机器代码的执行速度一样快,但是并没有说明为什么Microsoft认为这会提高性能。其原因是编译过程的最后一部分是在运行时进行的,JIT编译器确切地知道程序运行在什么类型的处理器上,利用该处理器提供的任何特性或特定的机器代码指令来优化最后的可执行代码。传统的编译器会优化代码,但它们的优化过程是独立于代码所运行的特定处理器的。这是因为传统的编译器是在发布软件之前编译为内部机器可执行的代码。即编译器不知道代码所运行的处理器类型,例如该处理器是x86兼容处理器或Alpha处理器,这超出了基本操作的范围。例如Visual Studio 6优化了一台一般的Pentium机器,所以它生成的代码就不能利用Pentium III处理器的硬件特性。相反,JIT编译器不仅可以进行Visual Studio 6所能完成的优化工作,还可以优化代码所运行的特定处理器。3. 语言的互操作性使用IL不仅支持平台无关性,还支持语言的互操作性。简言之,就是能将任何一种语言编译为中间代码,编译好的代码可以与从其他语言编译过来的代码进行交互操作。那么除了C#之外,还有什么语言可以通过.NET进行交互操作呢?下面就简要讨论其他常见语言如何与.NET交互操作。(1) VB.NETVisual Basic 6在升级到Visual Basic .NET时,经历了一番脱胎换骨的变化。Visual Basic是在最近的几年中演化的,其早期版本Visual Basic 6并不适合运行.NET程序。例如,它与COM的高度集成,且只把事件处理程序作为源代码显示给开发人员,大多数后台代码不能用作源代码。另外,它不支持继承,Visual Basic使用的标准数据类型也与.NET不兼容。Visual Basic 6已经升级为Visual Basic .NET,对VB进行的改变非常大,完全可以把Visual Basic .NET当作是一种新语言。现有的VB6代码不能编译为VB.NET代码,把VB6程序转换为VB.NET时,需要对代码进行大量的改动,但大多数修改工作都可以由Visual Studio .NET(VS的升级版本,用于与.NET一起使用)自动完成。如果要把一个VB6项目读取到Visual Studio .NET中,Visual Studio .NET就会升级该项目,也就是说把VB6源代码重写为VB.NET源代码。虽然这意味着其中的工作已大大减轻,但用户仍需要检查新的VB.NET代码,以确保项目仍可正确工作,因为这种转换并不十分完美。这种语言升级的一个副作用是不能再把VB.NET编译为内部可执行代码了。VB.NET只编译为中间语言,就像C#一样。如果需要继续使用VB6编写程序,就可以这么做,但生成的可执行代码会完全忽略.NET Framework,如果继续把Visual Studio作为开发环境,就需要安装Visual Studio 6。(2) Visual C+ .NETVisual C+ 6有许多Microsoft对Windows的特定扩展。通过Visual C+ .NET,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年国际供应链管理师资格考试试题及答案解析
- 2025年公共关系策划师职业技能水平考核试题及答案解析
- 2025年安全员B17考试模拟题库
- 2025年安全生产管理实操培训题集
- 护理教学课件资源有哪些
- 2025年渗透测试初级面试模拟题集宝典
- 2025年安全员招聘面试问题及答案解析
- 2025年村级水管员供水管理笔试题库
- 2025年机关节能面试模拟题及解析
- 2025年安全员C证复审强化复习题库
- 医院病历单请假用
- 肝胆外科专科知识题库及答案
- 《数字媒体基础与实践》数字媒体技术概述
- 滁州市珠龙广卫绢云母粉厂滁州市南谯区将军山绢云母矿1万吨-年露天采矿工程项目环境影响报告书
- 迷你中长导管-
- 钢质防火门安装施工方法
- 优化物理教学策略的思考(黄恕伯)
- GB/T 26358-2022旅游度假区等级划分
- GB/T 25146-2010工业设备化学清洗质量验收规范
- GB/T 14825-1993农药可湿性粉剂悬浮率测定方法
- GB/T 12008.7-2010塑料聚醚多元醇第7部分:黏度的测定
评论
0/150
提交评论