简体   繁体   English

在C ++中序列化然后在C#中反序列化?

[英]Serialize in C++ then deserialize in C#?

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. 我正在使用一些不能运行.Net的远程WINNT机器。 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). 我的服务器应用程序完全用C#编写,所以我想要一种简单的方法来共享简单的数据(主要是键值对,也许是SQL结果集的一些表示)。 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. 我认为最好的方法是在客户端上以某种预定义格式将数据写入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 ac# memory stream that I can deserialize into ac# object (eliminating file creation, transfer, etc), but I don't think anything like that exists. 客户端和服务器通过tcp连接进行通信,我真正想要的是将客户端内存中的数据序列化,通过套接字将二进制数据传输到ac#内存流,我可以将其反序列化为ac#对象(消除文件)创造,转移等),但我不认为这样的事情存在。 Feel free to enlighten me. 随意赐教。

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. 我知道我可以在c ++应用程序中创建一个结构并在c#中定义它并以这种方式传输数据,但在我看来,这就像我限制了可以发送的内容。 I'd have to set predefined sizes for objects, etc 我必须为对象等设置预定义的大小

Protocol Buffers might be useful to you. 协议缓冲区可能对您有用。

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler . 协议缓冲区是Google的语言中立,平台中立,可扩展的机制,用于序列化结构化数据 - 想想XML,但更小,更快,更简单 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. 您可以定义数据的结构化时间,然后可以使用特殊生成的源代码轻松地在各种数据流中使用各种语言(Java,C ++或Python)编写和读取结构化数据。

.NET ports are available from Marc Gravell and Jon Skeet . .NET端口可从Marc GravellJon Skeet获得

I checked out all mentioned projects like prottocol buffers, json, xml, etc. but after I have found BSON I use this because of the following reasons: 我检查了所有提到的项目,如prottocol缓冲区,json,xml等,但在我找到BSON之后我使用它,原因如下:

  • Easy to use API 易于使用的API
  • Available in many languages (C, C++, Haskell, Go, Erlang, Perl, PHP, Python, Ruby, C#, ...) 提供多种语言版本 (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) 在平台上保持一致(没有endianess等问题)
  • hierarchical. 分层。 The data model is comparable to json (what the name suggests) so most data modelling tasks should be solvable. 数据模型与json(顾名思义)相当,因此大多数数据建模任务都应该是可解决的。
  • No precompiler necessary 不需要预编译器
  • wideley used (Mongodb, many languages) wideley使用(Mongodb,多种语言)

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

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. C ++没有结构内省(你无法在运行时找到类的字段),因此没有编写C ++对象的通用机制。 You either have to adopt a convention and use code generation, or (more typically) write the serialisation yourself. 您必须采用约定并使用代码生成,或者(更典型地)自己编写序列化。

There are some libraries for standard formats such as ASN.1, HDF5, and so on which are implementation language neutral. 有一些标准格式的库,如ASN.1,HDF5等,它们是实现语言中立的。 There are proprietary libraries which serve the same purpose (eg protocol buffers). 存在用于相同目的的专有库(例如协议缓冲器)。

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),还是简单的普通数据对象(直接在内存中写入值)

Other options would be: 其他选择是:

  • creating a binary file that contains the data in the way you need it ( not a easy & portable solution ) 以您需要的方式创建包含数据的二进制文件(不是简单易用的解决方案)

  • XML XML

  • YAML YAML

  • plain text files 纯文本文件

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类的支持,你可以正常使用它们。

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: 快速谷歌搜索给出了以下内容:

Named pipes 命名管道

Named Shared Memory 命名共享内存

DDE DDE

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

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