深入了解 IL 对 .net 开发很重要
当我主要做 C++ 时,我认为了解汇编是至关重要的,并编写了一些非试验性的 asm 代码,这样我才能真正理解正在发生的事情。我现在主要使用 .net,虽然我对 IL 有一些了解,但我绝不精通。
IL 是一项人们应该了解的技能吗?或者更全面地学习 IL 会继续指导我如何使用和编写 .net 代码吗?
When I did mostly c++ I though it was critical to know assembly and wrote some non trial asm code just so that I could truly understand what was going on. I now do mostly .net and while I have some understanding of IL I am by no means proficient.
Is IL a skill that one should have a knowledge of or will learning IL more completely go on to inform how I use and write .net code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在很多方面,完全学习 IL 就像学习汇编一样。
了解 IL 可以帮助您了解实际编译 .NET 代码时会发生什么。这可能非常有帮助,特别是当您遇到性能关键分析情况时(您发现了真正的问题,并且需要对其进行优化)。
然而,对于大多数“日常”编程任务,我认为实际上没有必要了解 IL,就像大多数 C++ 程序员并不真正需要知道如何编写汇编一样。
话虽这么说,如果您了解具有 C++ 背景的汇编代码,那么获得 IL 的总体概念将非常非常容易......
Learning IL completely is like learning assembly, in many ways.
Having an understanding of IL can help you understand what happens when you're actually compiling your .NET code. This can be very helpful, especially if you ever run into performance critical profiling situations (where you've found a real problem, and need to optimize it).
However, for most "day to day" programming tasks, I don't think it's really necessary to understand the IL, just like most C++ programmers don't really need to know how to write assembly.
That being said, if you understand assembly code with a C++ background, getting a general idea of IL will be very, very easy...
如果您只是尝试编写 .NET 应用程序,则不需要任何 IL 代码的工作知识。大多数 .NET 开发人员不知道如何编写 IL 代码。
如果您想了解到底发生了什么,IL 将帮助您。您可以使用它在机器上编写更高效的代码,这在您正在编写的 .NET 语言中可能并不明显。
If you are just trying to write a .NET app you don't need any working knowledge of IL code. Most .NET developers do not know how to write in IL code.
IL will help you if you are trying to understand what is truly going on. You can use it to write more efficient code on a machine, that might not be obvious in the .NET language you are writing.
除了显而易见的任务(实现虚拟机)之外,某些类型的任务需要对 IL 有相当透彻的了解。
对我个人而言,最有趣的方面之一是检查 IL 语言对即时编译器的影响。对比 JVM 和 CLI 的一个值得注意的例子是 CLI 具有非虚拟方法和直接调度的概念,它允许非优化(基线)编译器发出特别有效的调用。这种分析在 JVM 中的成本更高,因此基线编译器无法优化尽可能多的情况。
回答原来的问题:
一般来说,.NET 开发人员不需要担心学习 CIL。如果您的工作涉及运行时/动态生成的方法,那么您应该评估表达式 API 满足您需求的能力,因为它非常更可用/可维护。我花了几周时间使用 ILGenerator 生成 CIL。我花了一番功夫才转换为表达式,节省了一百多行代码并修复了我尚未弄清楚的旧错误。
对于实现 CLI 虚拟机或编写编译器(AOT 或 JIT)的任何人来说,详细了解 CIL 都非常重要。我发现它既有趣又有用,但在“成为一名高效开发人员需要了解的重要事情”的范围内,它并没有那么高。
Aside from the obvious (implementing a virtual machine), certain types of tasks demanded a pretty thorough understanding of IL.
One of the most interesting aspects to me personally is examining the impact of the IL language on the just-in-time compiler. One notable example contrasting the JVM and the CLI is the CLI has a notion of a non-virtual methods and direct dispatch, which allows especially efficient calls to be emitted by the non-optimizing (baseline) compiler. This analysis is more expensive in the JVM, so the baseline compiler can't optimize as many cases.
In answer to the original question:
In general, .NET developers should not need to worry about learning CIL. If your work involves runtime/dynamically-generated methods, then you should evaluate the ability of the Expressions API to meet your needs because it is tremendously more usable/maintainable. I spent a few weeks emitting CIL with the ILGenerator. It took me one sitting to convert to Expressions, saving over a hundred of lines of code and fixing old bugs I just hadn't figured out.
A detailed knowledge of CIL is very important for anyone implementing a CLI virtual machine or writing compilers (AOT or JIT). I find it both interesting and useful, but in the scope of "important things to know to be a productive developer," it's just not that high.
在 .NET 框架中,您实际上只有一处需要了解 IL,即 System.Reflection.Emit 命名空间中的类。它允许您动态生成代码,这在某些情况下很有用。
接触 IL 的最佳方法是通过 Ildasm.exe 工具,Reflector 也可以。运行它的一个非常方便的方法是将其添加到“工具”菜单中。工具+外部工具,添加。
您现在可以编写一些托管代码,编译并快速获取看一下生成的 IL。如果您以前接触过机器代码,您会发现这很容易。请记住,IL 是无类型的,相同的操作码可用于任何值类型。
下一步是看看 IL 如何转换为机器代码。开始调试,右键单击代码并选择“转到反汇编”。请注意,除非运行发布版本并启用 JIT 优化,否则您不会查看真实的机器代码。工具+选项,调试,取消选中“抑制模块加载时的 JIT 优化”。
There's only really one place in the .NET framework where you have to know IL, the classes in the System.Reflection.Emit namespace. It lets you generate code on the fly, that can be useful in select cases.
The best way to get exposure to IL is through the Ildasm.exe tool, Reflector could work too. A very convenient way to run it is by adding it to the Tools menu. Tools + External Tools, Add.
You can now write some managed code, compile and quickly take a look at the generate IL. You'll find it pretty easy going if you've been exposed to machine code before. Just keep in mind that IL is untyped, the same opcode is used for any value type.
The next step is to take a look at how IL gets translated to machine code. Start debugging, right-click the code and choose Go To Disassembly. Beware that you won't look at the real machine code unless you run the Release build and enable JIT optimization. Tools + Options, Debugging, untick "Suppress JIT optimization on module load".
视情况而定。了解 IL 可能会帮助您理解代码会变成什么。如果您对 IL 有很好的了解,您甚至可以在运行时创建新组件(如果需要)。这是必要的吗?可能不会。
Depends. Knowing IL will probably help you understand what your code turns into. If you have a good knowledge if IL, you can even create new components at run-time (if you need to). Is it essential? Probably not.
了解汇编对于 Visual Studio 中的 C/C++ 非常有帮助。你总是可以去反汇编看看发生了什么。
如果您在 C# 中尝试相同的操作,即“转到反汇编”,您仍然会转到本机代码,而不是 IL。从这个意义上说,IL 不如真正的汇编有用。
但是 IL 非常适合查看没有源代码的内部程序集。 (但是您可以使用 reflector )
Knowing assembly was very good for C/C++ in Visual Studio. You could always go to disassembly to see what is going on.
If you try the same in C#, i.e. "Go to disassembly" you still go to native code, not IL. In that sense IL is not as useful as real assembly.
But IL is good for looking inside assemblies that you don't have the source code too. (But you could use reflector for that)
不,就像大多数人说的那样。另外,要添加到他们的评论中,请查看您使用 C/C++ 的原因以及使用 .NET 及其语言(如 C#)的原因:
C/C++ 的诞生是为了让您能够以一种抽象的方式控制计算机。它几乎直接转换为汇编语言,并为您提供了强大的善(和恶)力量。鼓励使用指针之类的东西,并且没有垃圾收集。因此,您必须知道汇编语言代码正在做什么,因为您可以做什么。
.NET 语言旨在快速开发出色的应用程序并限制对这些应用程序造成的损害。它会阻止您使用 CPU 的实际权力(就像不鼓励使用指针一样,并且您在大多数情况下不必担心垃圾收集。)基本上,您可以造成的损害仅限于应用程序;与 CPU 关系不大(除非您涉足不安全代码,这些代码仅在极少数情况下使用[根据我的经验])。此外,.NET 代码会转换为 IL,然后 IL 会转换为汇编语言,依此类推……无论如何,您并不是直接处理汇编语言。
这些说,了解IL对于头部知识是有好处的,可以对你有一点帮助。但了解它并不会极大地帮助您创建出色的应用程序 - 对于大多数不在 Microsoft 工作的开发人员来说,这就是 .NET 语言的要点。
No, like most said. Plus, to add to their comments look at why you use C/C++ and why you use .NET and its languaes (liek C#):
C/C++ was made to give you power over a computer in a nice abstracted way. It is converted ALMOST directly into assembly language and gives you great power for good (and evil.) Things like pointers are encouraged and there is no garbage collection. Hence, you have to should know what the assembly language code is doing because of what you can do.
.NET languages were made to make great apps quickly and limit the damage you can do to these apps. It discourages you from using real power over the CPU (like pointers are discouraged and you need not worry about garbage collection for the most part.) Basically, the damage you can do is limited to the app; and little to do with the CPU (unless you dabble with unsafe code which is only used in rare cases [in my experience]). In addition, .NET code is converted to IL, then IL is converted to assembly and so on... you are not directly dealing with assembly language anyway.
These said, it is good to know IL for head knowledge and can help you a little. But knowing it will not greatly help you create great app- which is the point of .NET languages for the most develop not working at Microsoft.