C# 什么是编程中的托管或非托管代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/334326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:37:01  来源:igfitidea点击:

What is managed or unmanaged code in programming?

c#.netunmanageddefinitionmanaged

提问by Pokus

I am using a specific command in in my C# code, which works well. However, it is said to misbehave in "unmanaged" code.

我在我的 C# 代码中使用了一个特定的命令,效果很好。但是,据说它在“非托管”代码中行为不端。

What is managed or unmanaged code?

什么是托管或非托管代码?

采纳答案by Patrick Desjardins

Here is some text from MSDN about unmanaged code.

这是来自 MSDN 的一些关于非托管代码的文本。

Some library code needs to call into unmanaged code (for example, native code APIs, such as Win32). Because this means going outside the security perimeter for managed code, due caution is required.

一些库代码需要调用非托管代码(例如,本机代码 API,如 Win32)。因为这意味着要超出托管代码的安全范围,所以需要格外小心。

Here is some other complimentary explication about Managed code:

以下是有关托管代码的其他一些免费解释:

  • Code that is executed by the CLR.
  • Code that targets the common language runtime, the foundation of the .NET Framework, is known as managed code.
  • Managed code supplies the metadata necessary for the CLR to provide services such as memory management, cross-language integration, code access security, and automatic lifetime control of objects. All code based on IL executes as managed code.
  • Code that executes under the CLI execution environment.
  • 由 CLR 执行的代码。
  • 面向公共语言运行时(.NET Framework 的基础)的代码称为托管代码。
  • 托管代码为 CLR 提供必要的元数据,以提供诸如内存管理、跨语言集成、代码访问安全性和对象的自动生命周期控制等服务。所有基于 IL 的代码都作为托管代码执行。
  • 在 CLI 执行环境下执行的代码。

For your problem:

对于您的问题:

I think it's because NUnit execute your code for UnitTesting and might have some part of it that is unmanaged. But I am not sure about it, so do not take this for gold. I am sure someone will be able to give you more information about it. Hope it helps!

我认为这是因为 NUnit 为 UnitTesting 执行您的代码,并且其中可能有一些不受管理的部分。但我不确定,所以不要把它当作黄金。我相信有人会为您提供更多有关它的信息。希望能帮助到你!

回答by joegtp

Basically unmanaged code is code which does not run under the .NET CLR (aka not VB.NET, C#, etc.). My guess is that NUnit has a runner/wrapper which is not .NET code (aka C++).

基本上非托管代码是不在 .NET CLR 下运行的代码(也不是 VB.NET、C# 等)。我的猜测是 NUnit 有一个 runner/wrapper,它不是 .NET 代码(又名 C++)。

回答by annakata

Thisis a good article about the subject.

是一篇关于这个主题的好文章。

To summarize,

总结一下,

  1. Managed codeis not compiled to machine code but to an intermediate language which is interpreted and executed by some service on a machine and is therefore operating within a (hopefully!) secure framework which handles dangerous things like memory and threads for you. In modern usage this frequently means .NET but does not have to.
  1. 托管代码不是编译为机器代码,而是编译为一种中间语言,该语言由机器上的某些服务解释和执行,因此在(希望如此!)安全框架内运行,该框架为您处理诸如内存和线程之类的危险事物。在现代用法中,这通常意味着 .NET,但并非必须如此。

An application program that is executed within a runtime engine installed in the same machine. The application cannot run without it. The runtime environment provides the general library of software routines that the program uses and typically performs memory management. It may also provide just-in-time (JIT) conversion from source code to executable code or from an intermediate language to executable code. Java, Visual Basic and .NET's Common Language Runtime (CLR) are examples of runtime engines. (Read more)

在安装在同一台机器上的运行时引擎内执行的应用程序。没有它,应用程序就无法运行。运行时环境提供程序使用并通常执行内存管理的软件例程的通用库。它还可以提供从源代码到可执行代码或从中间语言到可执行代码的即时 (JIT) 转换。Java、Visual Basic 和 .NET 的公共语言运行时 (CLR) 是运行时引擎的示例。(阅读更多

  1. Unmanaged codeis compiled to machine code and therefore executed by the OS directly. It therefore has the ability to do damaging/powerful things Managed code does not. This is how everything used to work, so typically it's associated with old stuff like .dlls.
  1. 非托管代码被编译为机器代码,因此由操作系统直接执行。因此,它有能力做托管代码没有的破坏性/强大的事情。这就是过去一切工作的方式,所以通常它与 .dll 之类的旧东西相关联。

An executable program that runs by itself. Launched from the operating system, the program calls upon and uses the software routines in the operating system, but does not require another software system to be used. Assembly language programs that have been assembled into machine language and C/C++ programs compiled into machine language for a particular platform are examples of unmanaged code.(Read more)

自行运行的可执行程序。该程序从操作系统启动,调用并使用操作系统中的软件例程,但不需要使用另一个软件系统。已被汇编为机器语言的汇编语言程序和为特定平台编译为机器语言的 C/C++ 程序是非托管代码的示例。(阅读更多

  1. Nativecode is often synonymous with Unmanaged, but is not identical.
  1. 本机代码通常与非托管同义,但并不完全相同。

回答by leppie

NUnit loads the unit tests in a seperate AppDomain, and I assume the entry point is not being called (probably not needed), hence the entry assembly is null.

NUnit 在单独的 AppDomain 中加载单元测试,我假设入口点没有被调用(可能不需要),因此入口程序集为空。

回答by Vilx-

In as few words as possible:

用尽可能少的词:

  • managed code = .NET programs
  • unmanaged code = "normal" programs
  • 托管代码 = .NET 程序
  • 非托管代码 =“正常”程序

回答by chirag Makwana

Managed code runs inside the environment of CLR i.e. .NET runtime.In short all IL are managed code.But if you are using some third party software example VB6 or VC++ component they are unmanaged code as .NET runtime (CLR) does not have control over the source code execution of the language.

托管代码在 CLR 环境中运行,即 .NET 运行时。简而言之,所有 IL 都是托管代码。但如果您使用某些第三方软件示例 VB6 或 VC++ 组件,则它们是非托管代码,因为 .NET 运行时 (CLR) 无法控制在语言的源代码执行上。

回答by TJPowell

When you think of unmanaged, think machine-specific, machine-level code. Like x86 assembly language. Unmanaged (native) code is compiled and linked to run directly on the processor it was designed for, excluding all the OS stuff for the moment. It's not portable, but it is fast. Very simple, stripped down code.

当您想到unmanaged 时,请考虑机器特定的机器级代码。像 x86 汇编语言。非托管(本机)代码经过编译和链接后可以直接在其设计的处理器上运行,目前不包括所有操作系统内容。它不便携,但速度很快。非常简单,精简的代码。

Managedcode is everything from Java to old Interpretive BASIC, or anything that runs under .NET. Managed code typically is compiled to an intermediate level P-Code or byte code set of instructions. These are not machine-specific instructions, although they look similar to assembly language. Managed code insulates the program from the machine it's running on, and creates a secure boundary in which all memory is allocated indirectly, and generally speaking, you don't have direct access to machine resources like ports, memory address space, the stack, etc. The idea is to run in a more secure environment.

托管代码是从 Java 到旧的 Interpretive BASIC 或任何在 .NET 下运行的代码。托管代码通常被编译为中间级别的 P 代码或字节代码指令集。这些不是特定于机器的指令,尽管它们看起来类似于汇编语言。托管代码将程序与其运行的机器隔离,并创建一个安全边界,其中所有内存都是间接分配的,一般来说,您无法直接访问机器资源,如端口、内存地址空间、堆栈等. 的想法是在更安全的环境中运行。

To convert from a managed variable, say, to an unmanaged one, you have to get to the actual object itself. It's probably wrapped or boxed in some additional packaging. UNmanaged variables (like an 'int', say) - on a 32 bit machine - takes exactly 4 bytes. There is no overhead or additional packaging. The process of going from managed to unmanaged code - and back again - is called "marshaling". It allows your programs to cross the boundary.

要从托管变量转换为非托管变量,您必须获得实际对象本身。它可能被包裹或装在一些额外的包装中。非托管变量(比如“int”)——在 32 位机器上——正好需要 4 个字节。没有开销或额外的包装。从托管代码到非托管代码——然后再返回——的过程称为“编组”。它允许您的程序跨越边界。

回答by Ashok Narwal

Managed Code :- Code which MSIL (intermediate language) form is developed after the language compiler compilation and directly executed by CLRcalled managed code. eg:- All 61 language code supported by .net framework

托管代码:- MSIL(中间语言)形式的代码在语言编译器编译后开发并由被CLR调用的托管代码直接执行。例如:- .net 框架支持的所有 61 种语言代码

Unmanaged Code:- code that developed before .netfor which MSIL form is not available and it is executed by CLRdirectly rather CLRwill redirect to operating system this is known as unmanaged code.

非托管代码:-之前开发的代码,.net其 MSIL 形式不可用,它由CLR直接执行而不是CLR重定向到操作系统,这称为非托管代码。

eg:-COM,Win32 APIs

例如:-COM、Win32 API

回答by Saket Kumar

  • ManagedCode: code written in .NET language like C#, VB.NET.
  • UnManagedCode: code not written in .NET language and MSIL does not understand what it is and can not run under CLR; like third-party controls we used in our .NET applications which is not created in .NET languages.
  • 托管代码:使用 C#、VB.NET 等 .NET 语言编写的代码。
  • UnManagedCode:不是用.NET语言编写的代码,MSIL不理解它是什么,不能在CLR下运行;就像我们在 .NET 应用程序中使用的第三方控件一样,这些控件不是用 .NET 语言创建的。

回答by GMalla

Managed Code:
Code that runs under a "contract of cooperation" with the common language runtime. Managed code must supply the metadata necessary for the runtime to provide services such as memory management, cross-language integration, code access security, and automatic lifetime control of objects. All code based on Microsoft intermediate language (MSIL) executes as managed code.

Un-Managed Code:
Code that is created without regard for the conventions and requirements of the common language runtime. Unmanaged code executes in the common language runtime environment with minimal services (for example, no garbage collection, limited debugging, and so on).

托管代码:
在与公共语言运行时的“合作合同”下运行的代码。托管代码必须提供运行时所需的元数据,以提供诸如内存管理、跨语言集成、代码访问安全性和对象的自动生命周期控制等服务。所有基于 Microsoft 中间语言 (MSIL) 的代码都作为托管代码执行。

非托管代码:
在不考虑公共语言运行时的约定和要求的情况下创建的代码。非托管代码在公共语言运行时环境中以最少的服务执行(例如,没有垃圾收集、有限的调试等)。

Reference: http://www.dotnetspider.com/forum/11612-difference-between-managed-and-unmanaged-code.aspx

参考:http: //www.dotnetspider.com/forum/11612-difference-between-managed-and-unmanaged-code.aspx