c# - 如何在 Python 中反序列化序列化的 C# 二进制文件?
问题描述
我想反序列化在 Visual Studio 2017 中用 C# 制作的二进制文件。
一个程序(C#)序列化structs
不同类型的arrays
文件,另一个程序(C#)反序列化文件。我想知道如何在 Python 中做同样的工作。
// ClassTickStruct.dll
namespace ClassTickStruct
{
[Serializable]
public struct struct_tick
{
public uint[] arr_currentIndex;
public string[] arr_currentType;
public DateTime[] arr_currentTime;
public struct_tick(byte size_index, byte size_type, byte size_time)
{
arr_currentIndex = new uint[size_index];
arr_currentType = new string[size_type];
arr_currentTime = new DateTime[size_time];
}
}
}
// How one program(C#) serializes structs
using (var fileStream = new FileStream(file.bin, FileMode.Append))
{
var bFormatter = new BinaryFormatter();
bFormatter.Serialize(fileStream, obj_struct);
}
// How the other program(C#) deserializes the file
List<struct_tick> list_read = new List<struct_tick>();
using (var fileStream = new FileStream("file.bin", FileMode.Open))
{
var bFormatter = new BinaryFormatter();
//bFormatter.Binder = new AllowAllAssemblyVersionsDeserializationBinder();
while (fileStream.Position != fileStream.Length)
{
list_read.Add((struct_tick)bFormatter.Deserialize(fileStream));
}
}
我提到了有没有办法将 C# 序列化对象读入 Python?并尝试了 DirtyLittleHelper 的代码,但出现错误:
data = serializer.Deserialize(reader)
System.Runtime.Serialization.SerializationException: 'tickReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' Assembly can't be found
location: System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
location: System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
location: System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
location: System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
location: System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
location: System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
location: System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
location: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
location: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
Assembly can't be found
当 C# 程序不共享结构的 dll 时,我遇到了同样的错误。然后我只是制作了一个包含结构的类库(dll),并让程序引用它,它们可以正常工作。
# the whole Python code to deserialize the file
import clr
import System
#requires pythonnet installed -> pip install pythonnet
clr.AddReference(r"C:\Users\null\Desktop\RECEIVER\ClassTickStruct.dll")
from System.Runtime.Serialization.Formatters.Binary import BinaryFormatter
from System.IO import FileStream,FileMode,FileAccess,FileShare
filepath = "file.bin"
serializer = BinaryFormatter()
reader = FileStream(filepath, FileMode.Open, FileAccess.Read)
data = serializer.Deserialize(reader)
我认为不同之处在于它(struct_tick)bFormatter.Deserialize(fileStream)
在 C# 中,而它只是serializer.Deserialize(reader)
在 Python 中。也许它需要对 dll 中的结构进行一些工作?
解决方案
我想要做的实际上可以使用 protobuf。
// WRITE
using (var fileStream = new FileStream("file.bin", FileMode.Append))
{
Serializer.Serialize(fileStream, OBJ_struct);
}
// READ
using (var fileStream = new FileStream("file.bin", FileMode.Open))
{
struct_obj str = Serializer.Deserialize<struct_obj >(fileStream);
for (int num=0; num < str.arr_currentIndex.Length; num++)
{
listBox1.Items.Add(str.arr_currentIndex[num]);
}
}
谢谢推荐protobuf的人!
推荐阅读
- go - 使用 Golang 和 Bootstrap 时,表单数据始终为空
- javascript - 是否可以将 MySQL 下的数据库导出到 Web 可访问的托管文件夹中?
- python-3.x - Selenium 找不到图标的元素
- javascript - 如何在 Javascript 中的对象构造函数中创建数组
- go - 管道 API 的 curl 命令
- php - WordPress 自定义侧边栏,传递唯一 ID
- python-3.x - Python Image/Pillow:如何使图像的背景更白
- apache - 从同一网络中的另一台机器访问本地域
- php - 定义一个可以从任何地方访问的值,没有减速
- statsmodels - 带有 statsmodels 的线性混合效应模型的收敛问题