为什么.NET代码可以编译为MSIL? [英] Why .NET code compiles to MSIL?

查看:70
本文介绍了为什么.NET代码可以编译为MSIL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,.NET代码编译为MSIL,然后JIT将其转换为与机器相关的代码。谁能告诉我所有的好处,因为两步编译。

First .NET code compiles to MSIL and then JIT convert it to machine dependent code. Can any one tell me what all benifits get because of the 2 step compilation.Thanks

推荐答案

原因很多。首先,最重要的是可能使其跨平台。如果将C#或其他.NET语言直接编译为本机代码,则必须针对它们所运行的每个平台重新编译它们。使用VM,所有代码都可以保留为中间格式,并且您只需要为每个平台编写一个VM实现。

There are a few reasons. First and foremost is probably to make it cross platform. If C# or other .NET languages compiled directly to native code, they would have to be recompiled for each platform they run on. With a VM, all code can be kept in an intermediate format, and you only need write a VM implementation for each platform.

此外,通过与语言无关的中介语言,您可以使用许多高级语言(C#,VB.NET,Python等)引用所有以其他语言编写的程序集。由于它们都可以编译为同一事物,因此它们可以彼此无缝地工作。

Also, by having a language-agnostic intermediary language, you can have many high level languages (C#, VB.NET, Python etc) all referencing assemblies written in other languages. Since they all compile into the same thing, they can work seamlessly with each other.

还有性能上的好处。 JIT编译器可以专门针对当时正在运行代码的机器进行积极的优化。我不知道.NET JIT编译器在这种意义上进行了多少优化,但是可能会有很大的理论收益。

There are also performance benefits. The JIT compiler can do aggressive optimizations specifically for the machine the code is running on at that time. I do not know how much optimization the .NET JIT compiler does in this sense, but there are very large theoretical benefits that could be had.

这篇关于为什么.NET代码可以编译为MSIL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆