编程中什么是托管代码或非托管代码?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
来自 Pro C# 5 和 .NET 4.5 Framework:
From Pro C# 5 and the .NET 4.5 Framework:
首先要明白这一点,在
.NET框架
之前,Microsoft
提供了独立的产品,如MFC (Visual C++)、VB、FoxPro
2002年,微软结合其产品,制作了.NET框架。 现在,以前的代码执行方式与 .NET 框架中的代码管理和执行方式有所不同。 微软在.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 likeMFC (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 likememory mangement, garbage collection
etc. But, such CLR features weren't available directly before.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.
这是一篇关于该主题的好文章。
总而言之,
This is a good article about the subject.
To summarize,
以下是来自 MSDN 的一些关于非托管代码的文本。
以下是有关托管代码的一些其他补充说明:
对于您的问题:
我认为这是因为 NUnit 执行您的 UnitTesting 代码,并且可能有一部分是非托管的。 但我对此并不确定,所以不要把它当成黄金。 我相信有人能够为您提供更多相关信息。 希望能帮助到你!
Here is some text from MSDN about unmanaged code.
Here is some other complimentary explication about Managed code:
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!
当您想到非托管时,请考虑特定于机器的机器级代码。 就像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.
简而言之:
In as few words as possible:
托管代码是 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.
基本上,非托管代码是不在 .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++).
不明白它是什么并且不能在CLR下运行; 就像我们在 .NET 应用程序中使用的第三方控件一样,这些控件不是用 .NET 语言创建的。
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.
参考: http://www.dotnetspider.com /forum/11612-托管和非托管代码之间的差异.aspx
Reference: http://www.dotnetspider.com/forum/11612-difference-between-managed-and-unmanaged-code.aspx
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.
托管代码在 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.
托管代码: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 byCLR
directly ratherCLR
will redirect to operating system this is known as unmanaged code.eg:-COM,Win32 APIs