C# 编组 - 它是什么以及我们为什么需要它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2240804/
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
Marshaling – what is it and why do we need it?
提问by
What is marshalling and why do we need it?
什么是编组,为什么我们需要它?
I find it hard to believe that I cannot send an int
over the wire from C# to C and have to marshall it. Why can't C# just send the 32 bits over with a starting and terminating signal, telling C code that it has received an int
?
我发现很难相信我不能通过int
线路从 C# 发送到 C 并且必须对其进行编组。为什么 C# 不能只发送带有开始和终止信号的 32 位,告诉 C 代码它已收到一个int
?
If there are any good tutorials or sites about why we need marshalling and how to use it, that would be great.
如果有关于为什么我们需要编组以及如何使用它的任何好的教程或网站,那就太好了。
采纳答案by jason
Because different languages and environments have different calling conventions, different layout conventions, different sizes of primitives (cf. char
in C# and char
in C), different object creation/destruction conventions, and different design guidelines. You need a way to get the stuff out of managed land an into somewhere where unmanaged land can see and understand it and vice versa. That's what marshalling is for.
因为不同的语言和环境有不同的调用约定、不同的布局约定、不同大小的基元(参见char
C# 和char
C)、不同的对象创建/销毁约定以及不同的设计指南。您需要一种方法将管理土地中的东西取出到未管理土地可以看到和理解的地方,反之亦然。这就是编组的目的。
回答by JSB????
Marshalling an int
is ideally just what you said: copying the memory from the CLR's managed stack into someplace where the C code can see it. Marshalling strings, objects, arrays, and other types are the difficult things.
编组 anint
理想情况下正是您所说的:将内存从 CLR 的托管堆栈复制到 C 代码可以看到它的某个地方。编组字符串、对象、数组和其他类型是困难的事情。
But the P/Invoke interop layer takes care of almost all of these things for you.
但是 P/Invoke 互操作层会为您处理几乎所有这些事情。
回答by t0mm13b
Marshalling is a "medium" for want of a better word or a gateway, to communicate with the unmanaged world's data types and vice versa, by using the pinvoke, and ensures the data is returned back in a safe manner.
编组是一种“媒介”,因为需要更好的词或网关,通过使用 pinvoke 与非托管世界的数据类型进行通信,反之亦然,并确保数据以安全的方式返回。
回答by Josh
As Vinko says in the comments, you can pass primitive types without any special marshalling. These are called "blittable" types and include types like byte, short, int, long, etc and their unsigned counterparts.
正如 Vinko 在评论中所说,您可以在没有任何特殊编组的情况下传递原始类型。这些被称为“blittable”类型,包括 byte、short、int、long 等类型及其无符号对应物。
This page contains the list of blittable and non-blittable types.
此页面包含blittable 和 non-blittable 类型的列表。
回答by Vojta
.NET code(C#, VB) is called "managed" because it's "managed" by CLR (Common Language Runtime)
.NET 代码(C#、VB)之所以称为“托管”,是因为它由 CLR(公共语言运行时)“托管”
If you write code in C or C++ or assembler it is all called "unmanaged", since no CLR is involved. You are responsible for all memory allocation/de-allocation.
如果您用 C 或 C++ 或汇编程序编写代码,则它都被称为“非托管”,因为不涉及 CLR。您负责所有内存分配/取消分配。
Marshalingis the process between managed code and unmanaged code; It is one of the most important services offered by the CLR.
编组是托管代码和非托管代码之间的过程;它是CLR提供的最重要的服务之一。
回答by Maryam Sheikh
Marshalling is passing signature of a function to a different process which is on a different machine, and it is usually implemented by conversion of structured data to a dedicated format, which can be transferred to other processor systems (serialization / deserialization).
编组是将函数的签名传递给不同机器上的不同进程,通常通过将结构化数据转换为专用格式来实现,该格式可以传输到其他处理器系统(序列化/反序列化)。