简体   繁体   English

是否可以反序列化C#中的对象,该对象已在C ++ MFC中进行二进制序列化?

[英]Is it possible to deserialize an object in C#, that has been binary serialized in C++ MFC?

The object has been serialized in C++ MFC using the CObject::Serialize() Method. 该对象已使用CObject::Serialize()方法在C ++ MFC中CObject::Serialize()

I would like to deserialize in C#. 我想在C#中反序列化。

Is that possible at all? 这有可能吗?

Yes, it's possible... 是的,有可能......

However, whether or not you'd want to do it is another thing. 但是,你是否想要这样做是另一回事。 The MFC serialization format is geared towards the MFC framework. MFC序列化格式面向MFC框架。 It encodes the names of MFC classes into the serialized data that you'd have to try find an equivilant C++ class for. 它将MFC类的名称编码为序列化数据,您必须尝试为其找到等效的C ++类。

Your best bet would be to use a language agnostic serialization format and use that to serialize your data. 您最好的选择是使用与语言无关的序列化格式并使用它来序列化您的数据。 JSON, XML or Protobuf are good fits for this. JSON,XML或Protobuf非常适合这种情况。

Is that possible at all? 这有可能吗?

Short answer: No. 简答:不。

MFC's de-serialization produces unmanaged objects on the unmanaged heap, and it needs access to the respective class' CRuntimeClass , as well as the framework's global structure mapping class names to factory methods. MFC的反序列化在非托管堆上生成非托管对象,它需要访问相应的类' CRuntimeClass ,以及框架的全局结构映射类名到工厂方法。 The latter, in particular, are not accessible from C#. 特别是后者无法从C#访问。

What you can do is read the serialized binary stream, and reconstruct the information to be stored in managed objects. 您可以做的是读取序列化二进制流,并重新构建要存储在托管对象中的信息。 The binary file format is documented under TN002: Persistent Object Data Format 1) . 二进制文件格式记录在TN002:持久对象数据格式 1)下 This will not allow you to serialize those managed objects back to an MFC-compatible binary stream (unless you re-implement the file format in your managed classes as well). 这将不允许您将这些托管对象序列化回MFC兼容的二进制流(除非您在托管类中重新实现文件格式)。


1) Note: The binary representation of each object in the serialized stream is encoded in its class' CObject::Serialize override. 1) 注意:序列化流中每个对象的二进制表示形式在其类' CObject :: Serialize override中编码。 Knowing the overall file format is not enough to de-serialize an MFC-serialized stream. 了解整体文件格式不足以反序列化MFC序列化流。 You also need to know each object's serialization code. 您还需要知道每个对象的序列化代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM