Why .NET code compiles to MSIL?

纵饮孤独 提交于 2019-12-21 11:47:08

问题


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


回答1:


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.




回答2:


The answer can be found at MSDN




回答3:


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.




回答4:


  • 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:

    • For instance OSX >= 10.5 compiles in missing GPU instructions at runtime with OpenCL.
    • Lets say you are working on a CPU without floating point support, then you could emulate it with the JIT without needing a complete code rewrite.
    • At some point in the future it could be possible to offload processing into the GPU or other targets dynamically (I suspect functional languages are somewhat better suited for this).


来源:https://stackoverflow.com/questions/1926386/why-net-code-compiles-to-msil

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!