在 C++ 中序列化然后在 C# 中反序列化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/726410/
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
Serialize in C++ then deserialize in C#?
提问by scottm
Is there an easy way to serialize data in c++ (either to xml or binary), and then deserialize the data in C#?
有没有一种简单的方法可以在 C++ 中序列化数据(到 xml 或二进制),然后在 C# 中反序列化数据?
I'm working with some remote WINNT machines that won't run .Net. My server app is written entirely in C#, so I want an easy way to share simple data (key value pairs mostly, and maybe some representation of a SQL result set). I figure the best way is going to be to write the data to xml in some predefined format on the client, transfer the xml file to my server, and have a C# wrapper read the xml into a usable c# object.
我正在使用一些无法运行 .Net 的远程 WINNT 机器。我的服务器应用程序完全是用 C# 编写的,所以我想要一种简单的方法来共享简单的数据(主要是键值对,也许还有一些 SQL 结果集的表示)。我认为最好的方法是在客户端以某种预定义格式将数据写入 xml,将 xml 文件传输到我的服务器,然后让 C# 包装器将 xml 读入可用的 c# 对象。
The client and server are communicating over a tcp connection, and what I really want is to serialize the data in memory on the client, transfer the binary data over the socket to a c# memory stream that I can deserialize into a c# object (eliminating file creation, transfer, etc), but I don't think anything like that exists. Feel free to enlighten me.
客户端和服务器通过 tcp 连接进行通信,我真正想要的是序列化客户端内存中的数据,通过套接字将二进制数据传输到 ac# 内存流,我可以将其反序列化为 ac# 对象(消除文件创建、转移等),但我认为不存在这样的事情。随意启发我。
Edit
编辑
I know I can create a struct in the c++ app and define it in c# and transfer data that way, but in my head, that feels like I'm limiting what can be sent. I'd have to set predefined sizes for objects, etc
我知道我可以在 c++ 应用程序中创建一个结构并在 c# 中定义它并以这种方式传输数据,但在我看来,这感觉就像我限制了可以发送的内容。我必须为对象等设置预定义的大小
采纳答案by Michael Myers
Protocol Buffersmight be useful to you.
Protocol Buffers可能对您有用。
Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages – Java, C++, or Python.
协议缓冲区是 Google 的语言中立、平台中立、可扩展的结构化数据序列化机制——想想 XML,但更小、更快、更简单。您可以定义一次数据的结构化方式,然后您可以使用特殊生成的源代码轻松地使用各种语言(Java、C++ 或 Python)在各种数据流之间写入和读取结构化数据。
.NET ports are available from Marc Gravelland Jon Skeet.
.NET 端口可从Marc Gravell和Jon Skeet 获得。
回答by Pete Kirkham
C++ doesn't have structural introspection (you can't find out the fields of a class at runtime), so there aren't general mechanisms to write a C++ object. You either have to adopt a convention and use code generation, or (more typically) write the serialisation yourself.
C++ 没有结构自省(您无法在运行时找出类的字段),因此没有编写 C++ 对象的通用机制。您要么必须采用约定并使用代码生成,要么(更常见的是)自己编写序列化。
There are some libraries for standard formats such as ASN.1, HDF5, and so on which are implementation language neutral. There are proprietary libraries which serve the same purpose (eg protocol buffers).
有一些标准格式的库,例如 ASN.1、HDF5 等,它们与实现语言无关。有用于相同目的的专有库(例如协议缓冲区)。
If you're targeting a particular architecture and compiler, then you can also just dump the C++ object as raw bytes, and create a parser on the C# side.
如果您的目标是特定的体系结构和编译器,那么您也可以将 C++ 对象转储为原始字节,并在 C# 端创建一个解析器。
Quite what is better depends how tightly coupled you want your endpoints to be, and whether the data is mainly numerical (HDF5), tree and sequence structures (ASN.1), or simple plain data objects (directly writing the values in memory)
什么更好取决于您希望端点的耦合程度,以及数据主要是数字(HDF5)、树和序列结构(ASN.1)还是简单的纯数据对象(直接将值写入内存)
回答by Geo
Other options would be:
其他选择是:
creating a binary file that contains the data in the way you need it ( not a easy & portable solution )
XML
YAML
plain text files
创建一个以您需要的方式包含数据的二进制文件(不是一个简单且便携的解决方案)
XML
YAML
纯文本文件
回答by Vinay
There are a lot of options you can choose from. Named pipes, shared memory, DDE, remoting... Depends on your particular need.
有很多选项可供您选择。命名管道、共享内存、DDE、远程处理……取决于您的特定需求。
Quick googling gave the following:
快速谷歌搜索给出了以下内容:
回答by casperOne
As mentioned already, Protocol Buffers are a good option.
如前所述,Protocol Buffers 是一个不错的选择。
If that option doesn't suit your needs, then I would look at sending the XML over to the client (you would have to prefix the message with the length so you know how much to read) and then using an implementation of IXmlSerializer or use the DataContract/DataMember attributes in conjunction with the DataContractSerializer to get your representation in .NET.
如果该选项不适合您的需要,那么我会考虑将 XML 发送到客户端(您必须在消息前面加上长度前缀,以便您知道要读取多少),然后使用 IXmlSerializer 的实现或使用DataContract/DataMember 属性与 DataContractSerializer 结合使用以在 .NET 中获得您的表示。
I would recommend against using the marshaling attributes, as they aren't supported on things like List<T>
and a number of other standard .NET classes which you would use normally.
我建议不要使用封送处理属性,因为它们不受诸如List<T>
通常使用的许多其他标准 .NET 类的支持。
回答by patrick
WCF is the .NET framework solution for serializing data it looks like this link figured out how to consume the data in c++, that's kind of the opposite direction of what you want, but there might be an easy analog for reversal.
WCF 是用于序列化数据的 .NET 框架解决方案,它看起来像这个链接想出了如何在 C++ 中使用数据,这与您想要的方向相反,但可能有一个简单的逆转模拟。
http://geekswithblogs.net/cicorias/archive/2007/08/28/Consuming-WCF-Services-from-COM-using-C.aspx
http://geekswithblogs.net/cicorias/archive/2007/08/28/Consuming-WCF-Services-from-COM-using-C.aspx
回答by schoetbi
I checked out all mentioned projects like prottocol buffers, json, xml, etc. but after I have found BSONI use this because of the following reasons:
我检查了所有提到的项目,如协议缓冲区、json、xml 等,但在我找到BSON 后,我使用它是因为以下原因:
- Easy to use API
- Available in many languages(C, C++, Haskell, Go, Erlang, Perl, PHP, Python, Ruby, C#, ...)
- Binary therefore very space efficient and fast (less bytes->less time)
- constistent over platforms (no problems with endianess, etc)
- hierarchical. The data model is comparable to json (what the name suggests) so most data modelling tasks should be solvable.
- No precompiler necessary
- wideley used (Mongodb, many languages)
- 易于使用的 API
- 支持多种语言(C、C++、Haskell、Go、Erlang、Perl、PHP、Python、Ruby、C#、...)
- 二进制因此非常节省空间和快速(更少的字节->更少的时间)
- 在平台上保持一致(endianess 等没有问题)
- 层次分明。数据模型类似于 json(顾名思义),因此大多数数据建模任务应该是可解决的。
- 不需要预编译器
- 使用wideley(Mongodb,多种语言)