本文介绍了二进制序列化/反序列化在C ++和C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个分布式应用程序,它有两个组件。一个用标准C ++(不是托管C ++ )编写,在Linux平台上运行,另一个用C#编写。两者都通过消息总线进行通信。



我有一种情况,我需要将对象从C ++传递到C#应用程序,为此我需要序列化这些对象在C +并在C#中去序列化它们(类似于.NET中的封送/解封装)。我需要以二进制而不是XML(由于性能原因)执行此序列化。



我使用 Boost.Serialization 当两端都在C ++中实现,但现在我有一个.NET应用程序在一端, Boost.Serialization 是不可行的解决方案。 p>

我正在寻找一个解决方案,允许我在C ++和.NET边界(即跨平台二进制序列化)中执行(de)序列化。 p>

我知道我可以在C ++ dll中实现(de)序列化代码,并在.NET中使用 P / Invoke 应用程序,但我想保持作为最后的手段。



此外,我想知道如果我使用一些标准,如gzip,这将是高效的吗? gzip有其他替代方法吗?



感谢

解决方案

gzip不会直接帮助序列化 - 它只会(试图)缩小流。根据流中重复的数据量,此可以帮助或不帮助。对于少量文本的密集数据,我已经看到gzip 增加有效负载的大小。



个人我会看看在这里(但我有偏见,因为我是)。通常(但不总是)以基本语言(.proto文件)定义消息,并运行特定于语言的工具来生成类。性能非常好 - 专注于.NET它可以超过内置的序列化器( )


I am working on a distributed application which has two components. One is written in standard C++ (not managed C++ and running on a Linux platform) and the other one is written in C#. Both are communicating via a message bus.

I have a situation in which I need to pass objects from C++ to C# application and for this I need to serialize those objects in C++ and de-serialize them in C# (something like marshaling/un-marshaling in .NET). I need to perform this serialization in binary and not in XML (due to performance reasons).

I have used Boost.Serialization to do this when both ends were implemented in C++ but now that I have a .NET application on one end, Boost.Serialization is not a viable solution.

I am looking for a solution that allows me to perform (de)serialization across C++ and .NET boundary i.e., cross platform binary serialization.

I know I can implement the (de)serialization code in a C++ dll and use P/Invoke in the .NET application, but I want to keep that as a last resort.

Also, I want to know if I use some standard like gzip, will that be efficient? Are there any other alternatives to gzip? What are the pros/cons of them?

Thanks

解决方案

gzip won't directly help with serialization - it will just (attempt to) shrink a stream. This may help, or not, depending on the amount of duplicated data in the stream. For dense data with little text, I've seen gzip increase the size of the payload.

Personally I would look at protocol buffers here (but I'm biased, as I'm one of the authors of the many extensions). You typically (but not always) define the messages in a basic language (a .proto file), and run language-specific tools to generate the classes. Performance is very good - focusing on .NET it can far exceed the inbuilt serializers (1 2 3 )

这篇关于二进制序列化/反序列化在C ++和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:36