Last week a young student ask me if marshalling is the same as casting. My answer was definetly no. Marshalling is seralization, the way to transform a memory representation of an objet into bytes in order to be transmitted to a network whereas casting is related to type convertion / coercion.
上周,一名年轻学生问我编组是否与铸造相同。我的答案绝对没有。编组是一种分区,即将对象的内存表示转换为字节以便传输到网络的方式,而转换与类型转换/强制有关。
Later on, rethinking the question I was thought that marshalling can be seen as a special case of casting. For example the transformation of the memory representation is in xml then one can say that you are "casting" to the type defined by the corresponding xsd grammar of that xml file.
后来,重新思考这个问题,我认为编组可以看作是一个特殊的铸造案例。例如,内存表示的转换是在xml中,然后可以说你正在“转换”到由该xml文件的相应xsd语法定义的类型。
What do you think about this?
你怎么看待这件事?
3 个解决方案
#1
Casting doesn't modify the data type. That is a major distinction. When you marshal something, you are transforming the data into something else.
转换不会修改数据类型。这是一个重要的区别。当你编组某些东西时,你正在将数据转换成其他东西。
A simple cast only changes how you are interpreting the object, not what the object is internally.
简单的强制转换只会改变您解释对象的方式,而不会改变对象的内部对象。
I agree that the distinction should be clear else unfamiliar people may be confused.
我同意区别应该是明确的,否则不熟悉的人可能会感到困惑。
#2
Marshalling is generally about a technology boundary (e.g. going across a network or from one memory type to another as in the case of managed/unmanaged) whereas casting is generally within the same technology boundary therefore I think they are definitely different things.
编组通常是关于技术边界(例如,跨越网络或从一种存储器类型到另一种存储器类型,如在托管/非托管的情况下),而铸造通常在相同的技术边界内,因此我认为它们肯定是不同的东西。
It would be exceptionally confusing if we used the same term for both approaches meaning we would need to define them differently as they have different behaviours.
如果我们对两种方法都使用相同的术语,那将会异常混乱,这意味着我们需要以不同的方式定义它们,因为它们具有不同的行为。
#3
They're both a "type conversion", but, they are different kinds of type conversion: casting is usually between related object types (e.g. a downcast from a superclass to a subclass), whereas a marshalling might be for example from an object graph to plain-text representation.
它们都是“类型转换”,但是,它们是不同类型的转换:转换通常在相关对象类型之间(例如,从超类到子类的向下转换),而编组可能是例如来自对象图形到纯文本表示。
#1
Casting doesn't modify the data type. That is a major distinction. When you marshal something, you are transforming the data into something else.
转换不会修改数据类型。这是一个重要的区别。当你编组某些东西时,你正在将数据转换成其他东西。
A simple cast only changes how you are interpreting the object, not what the object is internally.
简单的强制转换只会改变您解释对象的方式,而不会改变对象的内部对象。
I agree that the distinction should be clear else unfamiliar people may be confused.
我同意区别应该是明确的,否则不熟悉的人可能会感到困惑。
#2
Marshalling is generally about a technology boundary (e.g. going across a network or from one memory type to another as in the case of managed/unmanaged) whereas casting is generally within the same technology boundary therefore I think they are definitely different things.
编组通常是关于技术边界(例如,跨越网络或从一种存储器类型到另一种存储器类型,如在托管/非托管的情况下),而铸造通常在相同的技术边界内,因此我认为它们肯定是不同的东西。
It would be exceptionally confusing if we used the same term for both approaches meaning we would need to define them differently as they have different behaviours.
如果我们对两种方法都使用相同的术语,那将会异常混乱,这意味着我们需要以不同的方式定义它们,因为它们具有不同的行为。
#3
They're both a "type conversion", but, they are different kinds of type conversion: casting is usually between related object types (e.g. a downcast from a superclass to a subclass), whereas a marshalling might be for example from an object graph to plain-text representation.
它们都是“类型转换”,但是,它们是不同类型的转换:转换通常在相关对象类型之间(例如,从超类到子类的向下转换),而编组可能是例如来自对象图形到纯文本表示。