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

发布于 2024-07-10 04:30:54 字数 80 浏览 4 评论 0原文

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

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

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

What is managed or unmanaged code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

三生殊途 2024-07-17 04:30:55

来自 Pro C# 5 和 .NET 4.5 Framework:

托管代码与非托管代码:
也许了解 C# 语言最重要的一点是它可以生成以下代码:
只能在 .NET 运行时内执行(您永远无法使用 C# 构建本机 COM 服务器或
非托管 C/C++ 应用程序)。 正式来说,该术语用于描述针对
.NET 运行时是托管代码。 包含托管代码的二进制单元称为程序集
(稍后会详细介绍组件)。 相反,不能直接由 .NET 托管的代码
运行时称为非托管代码。

From Pro C# 5 and the .NET 4.5 Framework:

Managed vs. Unmanaged Code:
Perhaps the most important point to understand about the C# language is that it can produce code that
can execute only within the .NET runtime (you could never use C# to build a native COM server or an
unmanaged C/C++ application). Officially speaking, the term used to describe the code targeting the
.NET runtime is managed code. The binary unit that contains the managed code is termed an assembly
(more details on assemblies in just a bit). Conversely, code that cannot be directly hosted by the .NET
runtime is termed unmanaged code.

冷情 2024-07-17 04:30:55

首先要明白这一点,在.NET框架之前,Microsoft提供了独立的产品,如MFC (Visual C++)、VB、FoxPro 2002年

,微软结合其产品,制作了.NET框架。 现在,以前的代码执行方式与 .NET 框架中的代码管理和执行方式有所不同。 微软在.NET框架中引入了CLR的概念,它可以编译来自.NET框架任何支持的语言的代码,并提供诸如内存管理、垃圾收集等附加功能。但是,以前无法直接使用此类 CLR 功能。

因此,如果您在 .NET 框架中创建库/代码(使用
CLR),则称为托管代码。 你可以使用这个库
进一步在其他 .NET 应用程序/项目中,CLR 也将
了解它之前是如何编译的,这就是为什么它仍然是你的
管理代码。

OTOH 如果你想使用 .NET Framework 之前编写的库,那么你可以做一些限制,但请记住,由于当时 CLR 不存在,所以现在 CLR 将无法理解并再次编译此代码。 这将被称为非托管代码。 请注意,由某些第三方创建的用于提供某些功能/工具的库/程序集如果不兼容 CLR,也可能被视为非管理代码。

通俗地说,管理代码是 CLR 可以理解的东西,并且可以自行编译它以供进一步执行。 在 .NET 框架中,(来自在 .NET 框架上工作的任何语言)当代码进入 CLR 时,代码会提供一些元数据信息,以便 CLR 可以为您提供指定的功能 此处。 其中很少有垃圾收集、性能改进、跨语言集成、内存管理等。OTOH

未托管代码是特定于机器的并且可以立即使用,无需进一步处理它。

First of all understand this, before .NET framework, Microsoft were providing the stand-alone products like MFC (Visual C++), VB, FoxPro etc.

In 2002, Microsoft combined its products and made .NET framework. Now there is a difference between how code was executed before and how code is managed and executed in .NET framework. Microsoft introduced concept of CLR with .NET framework which compiles the code coming from any supported lanugague of .NET framework and provide additional functionalities like memory mangement, garbage collection etc. But, such CLR features weren't available directly before.

So if you are creating library/code in .NET framework (compiled with
CLR) then that is called Managed code. You can use this library
further in other .NET application/project, and there too, CLR will
understand how it was compiled before, and that's why it remains your
manage code.

OTOH if you want to use the libraries that were written prior to .NET framework then you can do with certain limitations, but remember, since CLR wasn't there at that time, so now, CLR won't understand and compile this code again. And this will be called unmanaged code. Please note that, libraries/assembilies created by some third party to provide certain features/tool may also be considered as unmanage code if is not CLR compatiblie.

In laymen terms, Manage code is something which your CLR understands and can compile it on its own for further execution. In .NET framework, (from any language thats work on .NET framework) When code goes to CLR then code supply some meta data information, so that CLR can provide you features specified here. Few of them are Garbage collection, Performance improvements, cross-language integration, memory management etc.

OTOH, unmanged code is something specific to machine and ready to use, no need to process it further.

迷你仙 2024-07-17 04:30:54

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

总而言之,

  1. 托管代码不是编译为机器代码,而是编译为中间语言,由机器上的某些服务解释和执行,因此在处理危险事物的(希望如此!)安全框架内运行就像你的内存和线程一样。 在现代用法中,这通常意味着 .NET,但并非必须如此。

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

  1. 非托管代码被编译为机器代码,因此由操作系统直接执行。 因此,它有能力做托管代码所不具备的破坏性/强大的事情。 这就是过去一切的工作方式,因此通常它与 .dll 等旧内容相关。

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

  1. 本机代码通常与非托管代码同义,但并不完全相同。

This is a good article about the subject.

To summarize,

  1. Managed code is 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.

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)

  1. Unmanaged code is 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.

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)

  1. Native code is often synonymous with Unmanaged, but is not identical.
情绪 2024-07-17 04:30:54

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

某些库代码需要调用非托管代码(例如,本机代码 API,例如 Win32)。 因为这意味着要超出托管代码的安全范围,所以需要适当谨慎。

以下是有关托管代码的一些其他补充说明:

  • 由 CLR 执行的代码。
  • 面向公共语言运行时(.NET Framework 的基础)的代码称为托管代码。
  • 托管代码提供 CLR 所需的元数据,以提供内存管理、跨语言集成、代码访问安全性和对象的自动生命周期控制等服务。 所有基于 IL 的代码都作为托管代码执行。
  • 在CLI执行环境下执行的代码。

对于您的问题:

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

Here is some text from MSDN about unmanaged code.

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.

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.

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!

心在旅行 2024-07-17 04:30:54

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

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

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

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.

Managed code 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.

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.

⊕婉儿 2024-07-17 04:30:54

简而言之:

  • 托管代码 = .NET 程序
  • 非托管代码 =“正常”程序

In as few words as possible:

  • managed code = .NET programs
  • unmanaged code = "normal" programs
心凉 2024-07-17 04:30:54

托管代码是 C#.Net、VB.Net、F#.Net 等编译器创建的代码。 它在 CLR 上运行,除了其他功能外,CLR 还提供垃圾收集、引用检查等服务。 所以可以认为,我的代码是由 CLR 管理的。

另一方面,非托管代码直接编译为机器代码。 它不由 CLR 管理。

Managed code is what C#.Net, VB.Net, F#.Net etc compilers create. It runs on the CLR, which among other things offers services like garbage collection, reference checking, and much more. So think of it as, my code is managed by the CLR.

On the other hand, unmanaged code compiles straight to machine code. It's not managed by the CLR.

人心善变 2024-07-17 04:30:54

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

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++).

自在安然 2024-07-17 04:30:54
  • 托管代码:用.NET语言(例如C#、VB.NET)编写的代码。
  • 非托管代码:代码不是用 .NET 语言编写的,而 MSIL 是用 .NET 语言编写的
    不明白它是什么并且不能在CLR下运行; 就像我们在 .NET 应用程序中使用的第三方控件一样,这些控件不是用 .NET 语言创建的。
  • Managed Code: code written in .NET language like C#, VB.NET.
  • UnManaged Code: 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.
感性 2024-07-17 04:30:54

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

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

参考: http://www.dotnetspider.com /forum/11612-托管和非托管代码之间的差异.aspx

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).

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

岁月染过的梦 2024-07-17 04:30:54

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

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.

抽个烟儿 2024-07-17 04:30:54

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

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.

瑶笙 2024-07-17 04:30:54

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

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

例如:-COM、Win32 API

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

Unmanaged Code:- code that developed before .net for which MSIL form is not available and it is executed by CLR directly rather CLR will redirect to operating system this is known as unmanaged code.

eg:-COM,Win32 APIs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文