为什么.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有几个原因。首先也是最重要的可能是使其跨平台。如果 C# 或其他 .NET 语言直接编译为本机代码,则必须针对其运行的每个平台重新编译它们。使用VM,所有代码都可以以中间格式保存,您只需为每个平台编写VM实现即可。
此外,通过使用与语言无关的中间语言,您可以使用许多高级语言(C#、VB.NET、Python 等)来引用用其他语言编写的程序集。由于它们都编译成相同的东西,因此它们可以彼此无缝地工作。
还有性能优势。 JIT 编译器可以专门针对当时运行代码的机器进行积极的优化。我不知道 .NET JIT 编译器在这个意义上做了多少优化,但理论上可以带来很大的好处。
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.
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.
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.
答案可以在 MSDN 找到
The answer can be found at MSDN
首先从高级语言转换到机器级,这就是.Net平台的设计方式。第一层负责 MSIL 的高级语言,第二层可以专注于挂接和挂接。从 MSIL 转换为机器级代码的平台故障。它主要支持语言互操作性,并且可能在不久的将来,当像 Mono 这样的项目获得更多支持时,它还将提供跨平台支持。
First Conversion from High Level Language and then to Machine Level, this is how .Net platform is designed. The first layer take care of High Level language to MSIL and second level can concentrate on hitch & glitch of platform to convert from MSIL to machine level code. It mainly supports Language interoperability and may be in near future it will also provide Cross Platform support when Project like Mono will gain more ground.
可执行文件未绑定到平台。例如,XNA 同时针对 PPC (Xbox360) 和 x86 处理器。有些程序可以在 Linux 或 OSX 上的 Mono 上运行。
它可以让您更好地优化目标机器或替换缺失的功能:
An executable is not bound to the platform. For instance XNA targets both PPC (Xbox360) and x86 processors. Some programs will run on Mono on linux or OSX.
It allows you to better optimize for the target machine or replace missing functions: