.net 什么是编组?当某些东西被“编组”时会发生什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5600761/
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
What is marshalling? What is happening when something is "marshalled?"
提问by richard
I know this question has been asked, at least here.
我知道有人问过这个问题,至少在这里。
But there wasn't a satisfactory answer, at least not to me. There is a lot of talk about marshalling as regards interoperating with unmanaged code, but what about marshalling from one thread to another, as we have to do in .NET sometimes.
但是没有一个令人满意的答案,至少对我来说不是。关于与非托管代码互操作的编组有很多讨论,但是从一个线程到另一个线程的编组呢,就像我们在 .NET 中有时必须做的那样。
This makes me ask, what is marshalling, really? When you give a definition of marshalling, how would you define it so that it is explaining the case of interoperability, as well as the cases where you are "marshalling" between threads?
这让我问,什么是编组,真的?当您给出编组的定义时,您将如何定义它以解释互操作性的情况,以及您在线程之间“编组”的情况?
回答by Ira Baxter
Computations often need to move data from one site to another, and don't have any shared memory. So one computation sends a message containing the data to the other.
计算通常需要将数据从一个站点移动到另一个站点,并且没有任何共享内存。因此,一个计算向另一个计算发送包含数据的消息。
How should that data, if it is arbitrarily complicated, be sent in a message?
如果这些数据很复杂,应该如何在消息中发送?
Marshalling is the process of converting a data field, or an entire set of related structures, into a serialized string that can be sent in a message. To marshall a binary number, one might convert it to hexadecimal digit string, if the message format must be text. If the message will carry binary data, the binary number might be converted into 4 little-endian normalized binary bytes and sent that way. Pointers are harder; one often has to convert them into an abstract reference (e.g., a "node number") that is independent of the actual memory locations.
编组是将数据字段或整个相关结构集转换为可在消息中发送的序列化字符串的过程。要编组一个二进制数,如果消息格式必须是文本,则可以将其转换为十六进制数字字符串。如果消息将携带二进制数据,则二进制数可能会转换为 4 个小端规范化二进制字节并以这种方式发送。指针更难;人们通常必须将它们转换为独立于实际内存位置的抽象引用(例如,“节点号”)。
Of course, if you "marshall" data, you must eventually "unmarshall", which is the process of reading the serial stream and reconstructing the transmitted data (structure).
当然,如果你“编组”数据,最终必须“解组”,也就是读取串行流并重构传输的数据(结构)的过程。
Often there are (un)marshalling routines in a library that are used to accomplish this purpose, and sometimes there are even tools that will manufacture all the calls needed on the (un)marshalling routines to send/recieve the data.
库中通常有(un)编组例程用于实现此目的,有时甚至有工具可以制造(un)编组例程上发送/接收数据所需的所有调用。
回答by Reed Copsey
Marshalling is taking data, of some form, and translating it into a separate form. It's a very generic term, and used in many places with subtle differences in meaning.
编组正在获取某种形式的数据,并将其转换为单独的形式。这是一个非常通用的术语,在许多地方使用,但含义有细微差别。
For example, in .NET, the interop layer when you're working with native types "marshals" your data from the .NET type into the appropriate form to call the native method, then "marshals" the results back.
例如,在 .NET 中,当您使用本机类型时,互操作层会将您的数据从 .NET 类型“编组”到适当的形式以调用本机方法,然后“编组”返回结果。
As for "marshalling" between threads - Often, you'll need to have code to run on a different thread than the current one. For example, if you're using Windows Forms, you can't change a UI element on a threadpool thread, so you'll need to "marshal" the call back to the UI thread. This is done by creating a delegate, and passing the delegate back to the user interface thread via Control.Invoke (which uses a rather complex system to post this back to the proper synchronization context), which in turn runs the delegate on the user interface thread for you.
至于线程之间的“编组” - 通常,您需要让代码在与当前线程不同的线程上运行。例如,如果您使用的是 Windows 窗体,则无法更改线程池线程上的 UI 元素,因此您需要将调用“编组”回 UI 线程。这是通过创建一个委托,并通过 Control.Invoke 将委托传递回用户界面线程来完成的(它使用一个相当复杂的系统将其发布回正确的同步上下文),后者又在用户界面上运行委托线程给你。
回答by nlawalker
Wikipedia'sdefinition is actually pretty good.
维基百科的定义其实很好。
The overall concept of marshalling is the same as "serialization:" moving from an in-memory representation (which, in a way, is like no representation at all - when something is in memory it simply "exists") to a "hard copy" representation, whether that's XML or maybe a binary stream or something. However, depending on what you're doing, it can also imply some kind of transformation or translation to a target format.
编组的整体概念与“序列化”相同:从内存中的表示(从某种意义上说,就像根本没有表示——当某些东西在内存中时,它只是“存在”)到“硬拷贝” " 表示,无论是 XML 还是二进制流之类的。但是,根据您在做什么,它也可能意味着某种转换或转换为目标格式。
For process marshalling: one thread doesn't simply "call" another - data has to be packaged up and "sent" from one thread to another. Marshalling is the process of packaging that data (for example, data about the method you want to call, and its parameters).
对于进程编组:一个线程不会简单地“调用”另一个——数据必须打包并从一个线程“发送”到另一个线程。编组是打包该数据(例如,有关您要调用的方法及其参数的数据)的过程。
If you're marshalling in terms of interop, you are packaging up a method call and its parameters into a data structure that can be sent to a process/thread running the COM component. That package needs to be in a format that the COM component can understand.
如果您在互操作方面进行编组,则您将方法调用及其参数打包到一个数据结构中,该数据结构可以发送到运行 COM 组件的进程/线程。该包需要采用 COM 组件可以理解的格式。
回答by Lee Berger
From Wikipedia - Marshalling (computer science):
Marshalling (similar to serialization) is the process of transforming the memory representation of an object to a data format suitable for storage or transmission. It is typically used when data must be moved between different parts of a computer program or from one program to another.
编组(类似于序列化)是将对象的内存表示转换为适合存储或传输的数据格式的过程。它通常用于数据必须在计算机程序的不同部分之间或从一个程序移动到另一个程序时。
In the case of calling an unmanaged function from .NET, marshalling is used to convert .NET's data into data that the unmanaged function can consume. For instance, System.Stringis Unicode based, but that string might need to be converted to an ANSI string to be passed into a unmanaged C function.
在从 .NET 调用非托管函数的情况下,编组用于将 .NET 的数据转换为非托管函数可以使用的数据。例如,System.String基于 Unicode,但该字符串可能需要转换为 ANSI 字符串才能传递给非托管 C 函数。
For threading, marshalling typically refers to transfer of ownership of some data from one thread to another thread. For example, a program has two threads. The first thread reads data from the network, and the second thread computes that data. After the network thread reads some data it transfers (i.e., "marshals") the data over to computation thread to process. It might do this by writing the data to a queue shared between the two threads.
对于线程,编组通常是指将某些数据的所有权从一个线程转移到另一个线程。例如,一个程序有两个线程。第一个线程从网络读取数据,第二个线程计算该数据。在网络线程读取一些数据后,它会将数据传输(即“编组”)到计算线程进行处理。它可以通过将数据写入两个线程之间共享的队列来实现这一点。
Marshalling in threading almost always involves synchronization of the data being marshalled.
线程中的编组几乎总是涉及被编组的数据的同步。
回答by Mike Bailey
The way I understand marshaling is that it provides a way for you to transfer data in a consistent manner across various operating environments.
我对编组的理解是,它为您提供了一种在各种操作环境中以一致的方式传输数据的方法。
In the context of marshaling data from managed to unmanaged code, it's more or less the same.
在将数据从托管代码编组到非托管代码的上下文中,它或多或少是相同的。
I have some data, say an array of integers or any data type of my choosing, and I want to make it available for use within my C# code after my C++ code does some operations on it.
我有一些数据,比如一个整数数组或我选择的任何数据类型,我想让它在我的 C++ 代码对其进行一些操作后在我的 C# 代码中使用。
I can't just say "Hey, this is where the array is, do what you want" to the C# code. An array of ints in C++ may not be stored the same way as in C#. Marshaling let's us transmit this data in an environment independent manner so that either side sees the data the same exact way.
我不能只是对 C# 代码说“嘿,这是数组所在的位置,做你想做的事”。C++ 中的整数数组的存储方式可能与 C# 中的不同。编组让我们以独立于环境的方式传输这些数据,以便任何一方都能以完全相同的方式查看数据。
Another example would be in networking. You usually don't call this marshaling, but if you want to transmit it over the network, you have to typically transmit it in such a way that whoever receives it interprets the data the same way you do. Your computer could represent data in little endian order, and the other could represent it in big endian order.
另一个例子是网络。您通常不称其为封送处理,但如果您想通过网络传输它,您通常必须以这样一种方式传输它,即接收它的人以与您相同的方式解释数据。您的计算机可以以小端顺序表示数据,而另一台可以以大端顺序表示数据。
tl;dr: Marshaling provides you a way to consistently represent data across various operating environments
tl;dr:编组为您提供了一种在各种操作环境中一致表示数据的方法
回答by corsiKa
It's usually used in the context of "written in an XML format" but it could be marshalled to any format.
它通常用于“以 XML 格式编写”的上下文中,但它可以编组为任何格式。
2. To arrange, place, or set in methodical order.
(from American Heritage? Dictionary of the English Language)
So it means you're arranging the data in the methodical order/format you want. Often this is in XML format.
所以这意味着你正在按照你想要的有条不紊的顺序/格式排列数据。通常这是 XML 格式。

