简体   繁体   English

MSIL与.NET中的托管代码相同吗?

[英]Is MSIL same as Managed Code in .NET?

I am confused with MSIL and Managed Code are they same or different? 我对MSIL托管代码感到困惑他们是相同还是不同? I mean to say, what happens when we built our C# code? 我的意思是说,当我们构建C#代码时会发生什么?

Which one is right 哪一个是对的

C# Code → C# compiler → Managed Code → MSIL

or 要么

C# Code → C# compiler → MSIL

Please provide authentic reference or link in support of your answer. 请提供真实的参考或链接以支持您的答案。

You're confusing different things. 你混淆了不同的东西。 Managed code is code that's written in a managed language (C#, VB.NET, F# and many others) that's compiled into CIL (Common Intermediate Language, formerly Microsoft Intermediate Language or MSIL) and run in the managed environment of CLR. 托管代码是用托管语言(C#,VB.NET,F#和许多其他语言)编写的代码,编译成CIL(通用中间语言,以前的Microsoft中间语言或MSIL)并在CLR的托管环境中运行。

Unmanaged code, on the other hand is compiled directly into native code (also called assembly), it does not run on CLR. 另一方面,非托管代码直接编译为本机代码(也称为程序集),它不在CLR上运行。

When you build your C# code, it's compiled into CIL. 在构建C#代码时,它会被编译为CIL。 This is why you can use tools like ildasm or Reflector to inspect the compiled code. 这就是为什么你可以使用像ildasm或Reflector这样的工具来检查编译的代码。 What exactly happens when you execute the CIL code depends on circumstances. 执行CIL代码时到底发生了什么取决于具体情况。 It could be 它可能是

  1. compiled into native code “just in time” using the JIT compiler (the most common option). 使用JIT编译器(最常见的选项)“及时”编译成本机代码。
  2. executed using a pre-compiled native code (you can use NGEN to get that). 使用预编译的本机代码执行(您可以使用NGEN来获取​​)。
  3. directly interpreted; 直接解释; I think some version of .Net for Windows CE, or similar does that. 我认为某些版本的.Net for Windows CE或类似版本。

As noted in the comments, MSIL has been renamed to CIL. 如评论中所述,MSIL已更名为CIL。

C# Code --> C# compiler --> CIL

because 因为

CIL = Managed

When put in its context, there is also 在其背景下,也有

Native Code = Unmanaged

Note that Managed Code is something completely different, which refers to the source code . 请注意, Managed Code是完全不同的,它指的是源代码


If you want to extend it, then the corrected full process is: 如果要扩展它,那么更正的完整过程是:

.NET Code --Compiler--> CIL (Managed) --JIT/NGEN--> Native Code (Unmanaged)


References: 参考文献:

  1. Introducing C# (What is the .NET Framework? -> CIL and JIT -> First two sencences) 介绍C#(什么是.NET Framework? - > CIL和JIT - >前两个步骤)

  2. JIT / NGEN (Towards a Cost Model for Managed Code -> The Just-in-Time compiler -> The whole) JIT / NGEN(迈向管理代码的成本模型 - >即时编译器 - >整体)

Code written to run exclusively under the control of the CLR is called managed code. 编写为仅在CLR控制下运行的代码称为托管代码。 ( Source ). 来源 )。 So 所以

C# Code → C# compiler → CIL (formerly known as MSIL) C#代码→C#编译器→CIL(以前称为MSIL)

is correct. 是正确的。

When you write programs for Windows, you can write one of two types of code: managed and native. 为Windows编写程序时,可以编写以下两种代码之一:托管代码和本机代码。 Native code is compiled directly into a machine executible or .dll, whereas managed code is code that targets the .NET runtime (in other words its compiled form, termed an assembly, is a file containing CIL instructions). 本机代码直接编译为可执行机器或.dll,而托管代码是以.NET运行时为目标的代码(换句话说,其编译形式,称为程序集,是包含CIL指令的文件)。 At runtime, the CIL is converted into machine code by the JIT compiler and executed. 在运行时,CIL由JIT编译器转换为机器代码并执行。 The difference between managed and native has to do with what the language (and its compiler) targets. 托管和本机之间的区别与语言(及其编译器)所针对的内容有关。 Native code targets the machine itself because it's compiled into a set of direct machine instructions. 本机代码以机器本身为目标,因为它被编译成一组直接机器指令。 On the other hand, C# compilers compile your code into CIL because the C# language compiler targets the .NET runtime. 另一方面,C#编译器将您的代码编译为CIL,因为C#语言编译器以.NET运行时为目标。 C# could then be said to be a "managed language", and any code written in it "managed code". 然后可以说C#是一种“托管语言”,其中编写的任何代码都是“托管代码”。 For example: 例如:

C src  -> gcc.exe -> .exe containing a machine binary ->            processor
C# src -> csc.exe -> .exe assembly containing CIL     -> CLR/JIT -> processor

I think the reason the term "managed" is used is because the instructions forming your code are ultimately handled by the .NET runtime instead of being executed directly by the processor. 我认为使用术语“托管”的原因是因为构成代码的指令最终由.NET运行时处理,而不是由处理器直接执行。 The extra step of compiling source to CIL isn't in and of itself what makes your code managed: rather, the fact that your program never gets directly executed by the processor is. 将源代码编译到CIL的额外步骤本身并不是什么使得代码得到管理:相反,程序永远不会被处理器直接执行的事实。 The CIL is just what allows multiple different managed languages to target the same runtime. CIL正是允许多种不同托管语言以同一运行时为目标的原因。

I know this is a lot wordier than just saying "managed languages compile into CIL", but that's because I don't really think that is a completely accurate description as to what makes the code managed. 我知道这比仅仅说“托管语言编译成CIL”要多得多,但那是因为我并不认为这是对代码管理的完全准确的描述。 Java bytecode is analogous to CIL, but actual "Java processors" exist which can natively execute bytecode. Java字节码类似于CIL,但存在可以原生执行字节码的实际“Java处理器”。 Such a processor is also conceivable for .NET CIL, which means that code compiled into CIL and run on such a machine would then become native. 对于.NET CIL也可以设想这样的处理器,这意味着编译成CIL并在这样的机器上运行的代码将变为本机的。 The fact that your code must be handled through the CLR is what makes it managed. 您的代码必须通过CLR处理的事实才能使其得到管理。 The CIL just happens to be the format that the code must be converted to becore the runtime can execute it. CIL恰好是代码必须转换为的格式,因为运行时可以执行它。

I won't go into a lot of detail here, because others have already covered a lot of it, but I want to clarify some points that other answers have not really explained well or correctly. 我不会在这里详细介绍,因为其他人已经介绍了很多细节,但我想澄清一些其他答案尚未正确解释的要点。 This is largely pedantic, and for 99.99999% of the cases you can just think of "C# and CIL/MSIL is managed code", but there are some subtleties to this. 这在很大程度上是迂腐的,对于99.99999%的情况,您可以想到“C#和CIL / MSIL是托管代码”,但这有一些细微之处。

First, CIL/MSIL is known as an "intermediate language", which means that it's a computer language that is used as an intermediate step between the source code and the final native machine code. 首先,CIL / MSIL被称为“中间语言”,这意味着它是一种计算机语言,用作源代码和最终本机机器代码之间的中间步骤。 However, this does not necessary have to be the case. 但是,这不是必须的。 I have heard about experimental projects where they have created a CPU that executes CIL (or the java equivalent called Java Byte Code) directly without first converting it to a native form (basically, the CIL is the native form). 我听说过实验项目,他们创建了一个CPU,它直接执行CIL(或java等价的Java Byte Code)而不先将其转换为本机形式(基本上,CIL是本机形式)。

Managed Code refers to code that is "managed" by a managed runtime of some sort. 托管代码是指由某种托管运行时“管理”的代码。 This typically means the code is garbage collected, and has some kind of security layer that prevents things like buffer overflows and other kinds of problems that can occur in native code. 这通常意味着代码是垃圾收集的,并且具有某种安全层,可以防止缓冲区溢出和本机代码中可能出现的其他类型的问题。 In .net this is known as the Common Language Runtime (CLR) 在.net中,这被称为公共语言运行时(CLR)

In the old days, this used to be known as a "virtual machine" and why the java environment is called a JVM, although that term is now largely a misnomer. 在过去,这曾经被称为“虚拟机”以及为什么java环境被称为JVM,尽管该术语现在在很大程度上是用词不当。 Nowadays, with JIT (Just in time) compilers, there is no actual "virtual machine", but it's instead a layer of code that "wraps" the native compiled code to ensure you don't break the rules, and to clean up after your code. 如今,使用JIT(即时)编译器,没有实际的“虚拟机”,而是一层代码“包装”本机编译代码,以确保您不破坏规则,并在之后进行清理你的代码。 It also abstracts some of the platform specific things out so that the CIL doesn't have to worry about them. 它还抽象出一些特定于平台的东西,以便CIL不必担心它们。

So, Managed code refers to the concept of code running in a managed runtime. 因此,托管代码指的是在托管运行时中运行的代码的概念。 CIL is considered managed code when it is running in a runtime such as the .NET runtime environment. 当CIL在运行时(例如.NET运行时环境)中运行时,它被视为托管代码。

C# and VB.NET are often considered "Managed Languages" because they are typically compiled to CIL and run under a managed runtime. C#和VB.NET通常被认为是“托管语言”,因为它们通常被编译为CIL并在托管运行时下运行。 However, this does not necessarily have to be the case (although if following the letter of the specification, it probably does have to be the case). 但是,这不一定是这种情况(尽管如果遵循规范的字母,它可能必须是这种情况)。 For instance, there are compilers that will compile c# directly to native code without an intermediate level, and there are runtime interpreters for C# that do not compile the code at all, but rather "interpret" it at runtime. 例如,有些编译器会将c#直接编译为本机代码而没有中间级别,并且C#的运行时解释器根本不编译代码,而是在运行时“解释”它。

So the gist is that Managed code and CIL are two different things, but they are related. 因此,要点是托管代码和CIL是两个不同的东西,但它们是相关的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM