.NET 如何以及何时实际编译代码?
假设您使用 C#、VB 或任何 .NET 编写应用程序 当你点击构建时,它真的会编译你的代码吗? 我是这么认为的,直到我开始在我的一些程序集上使用 redgates 反射器并逐字查看我的代码。 我本来期望展开循环并进行大量优化,但什么也没有。
那么编译实际上什么时候发生呢? 我认为当它被构建时,代码变成IL(中间语言)并且当执行发生时,它被加载到CLR中? 它是否仅在 CLR 期间进行优化,而从不在构建时进行优化?
Let's say you write an app in C#, VB, anything with .NET
When you hit build, does it really compile your code? I thought so until I started using redgates reflector on some of my assemblies and saw my code verbatim. I would have expected loops to be unrolled and another plethora of optimizations, instead nothing.
So when does the compilation actually happen? I think when it is built, code become IL (Intermediary Language) and when execution occurs, it is loading in the CLR? Is it optimized during CLR only and never at build time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您在 VS 中编译时,
执行时:
我也将答案发布在这里,因为另一个问题与此无关...
When you compile in VS
When you execute:
I post the answer here too as the other question was not really about this...
它在编译时编译为 IL。 Reflector 的神奇之处在于它“理解”IL 并将其转换回 c#(或 VB.NET 或其他语言。在 Reflector 中的“选项”菜单下查看,您可以查看任何格式的程序集,包括 IL)。
在 Reflector 中,您实际上看不到原始代码。 您正在看到 IL 到 c# 的翻译。 大多数时候,这与您所写的内容非常相似,但有一些明显的迹象 - 例如,找到一个实现 auto-property:
您将看到实际编译的内容,如下所示:
It is compiled down to IL at, well, compile time. Reflector's magic is that it "understands" the IL and converts it back into c# (or VB.NET or whatever. Look under the Options menu in Reflector and you can view the assembly in any format, including the IL).
In Reflector, you actually are not seeing your original code. You are seeing a translation of the IL into c#. Most of the time that will be very similar to what you wrote, but there are some telltale signs - for example, find a place where you implemented an auto-property:
And you'll see what that actually compiles to, which is something like this: