有没有办法将C#序列化对象读入Python? [英] Is there a way to read C# serialized objects into Python?

查看:130
本文介绍了有没有办法将C#序列化对象读入Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含C#序列化对象的二进制文件。



我可以用python读取内容,但得到的结果类似于:

 'T\x00\x00\x00Test.Jobs.GenerateJobRequest,POC.Server\xca\x02-\xa2\x02\ t\x82\x01\x06\x1a\x04myahR\x1d\x08\xfe\xff\xff\xff\xff\xff\xff\xff\xff\ \x01\x12\x10Data Lite导出p\t\n\x16Do_Ko_Change-Job__ID_23\x10\x0c\x18\xa7\xb9\x18(\x012\x00:\x00H \xbc\x08')

是否有办法在python中反序列化此对象? p>

我同意这不是最佳解决方案,JSON,XML会更好。但是,我无法控制序列化数据的过程,我只是消费者。

解决方案

根据您的问题,目前尚不清楚您使用的是哪个版本的Python(CPython,Jython,IronPython)。但是我假设您使用的是CPython,对于IronPython来说,这是微不足道的。



有一个CPython库, Python .NET 。它充当.NET和Python之间的绑定,并且效果非常好。甚至泛型都受支持。尽管它似乎不再受到积极支持,但我已经使用了一段时间。



您将需要Visual Studio对其进行编译,但它可能会与Visual Studio Express一起使用(尽管我不知道)。

p>

有了这个,您可以导入任何.NET-dll。假设您可以在C#中反序列化它,那么您也应该也可以在Python中反序列化它。


I have a binary file containing C# serialized objects.

I can read the contents with python, but get results similar to:

'T\x00\x00\x00Test.Jobs.GenerateJobRequest, POC.Server\xca\x02-\xa2\x02\t\x82\x01\x06\x1a\x04myahR\x1d\x08\xfe\xff\xff\xff\xff\xff\xff\xff\xff\x01\x12\x10Data Lite Exportp\t\n\x16Do_Ko_Change-Job__ID_23\x10\x0c\x18\xa7\xb9\x18(\x012\x00:\x00H\xbc\x08')

Is there a way to deserialize this object in python?

I agree this is not an optimal solution and JSON, XML would be better. However, I do not have control of the process that serializes the data, I am only the consumer.

解决方案

From your question, it is unclear which version of Python (CPython, Jython, IronPython) you are using. But I assume you are using CPython, as for IronPython it would be trivial.

There is a library for CPython, Python .NET. It serves as a binding between .NET and Python, and works really nice. Even generics are supported. Though it seems not to be actively supported anymore, I've been using it for a while now. It works like a charm.

You'll need Visual Studio to compile it, but probably it will work with Visual Studio Express (though I do not know).

With this, you can import any .NET-dll. Assuming you can deserialize it in C#, you then should be able to deserialize it in Python as well.

这篇关于有没有办法将C#序列化对象读入Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆