如何在一个字节数组之间对一个装配对象进行序列化/反序列化 [英] how to serialize/deserialize an assembly object to and from a byte array

查看:93
本文介绍了如何在一个字节数组之间对一个装配对象进行序列化/反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设通过
编译代码字符串在内存中创建一个(可执行)程序集。然后,我想将此程序集
对象序列化为字节数组,然后将其存储在数据库中。然后,稍后我想从数据库中检索
并将字节数组
反序列化为程序集对象,然后调用程序集的条目
点。

Let's say a create an (executable) assembly in memory by compiling a code string. Then I want to serialize this assembly object into a byte array and then store it in a database. Then later on I want to retrieve the byte array from the database and deserialize the byte array back into an assembly object, then invoke the entry point of the assembly.

起初,我只是想像.net中的任何其他简单对象一样进行此序列化,但是显然,它不适用于汇编对象。程序集对象包含一个名为GetObjectData的方法,该方法获取重新实例化程序集所需的序列化数据。因此,对于如何将所有这些组合到我的场景中,我有些困惑。

At first I just tried to do this serialization like I would any other simple object in .net, however apparently that won't work with an assembly object. The assembly object contains a method called GetObjectData which gets serialization data necessary to reinstantiate the assembly. So I'm somewhat confused as to how I piece all this together for my scenario.

答案只需要显示如何获取组装对象,并将其转换为字节数组,将其转换回一个程序集,然后在反序列化的程序集上执行entry方法。

The answer only needs to show how to take an assembly object, convert it into a byte array, convert that back into an assembly, then execute the entry method on the deserialized assembly.

推荐答案

一个程序集更方便地表示为二进制dll文件。如果您想到的是 ,其余的问题就会消失。具体来说,请查看 Assembly.Load(byte [])进行 loading Assembly 从二进制。要将其写入为二进制文件,请使用 CompileAssemblyFromSource 并查看结果的 PathToAssembly -然后是 File.ReadAllBytes(path)从文件中获取二进制文件。

An assembly is more conveniently represented simply as a binary dll file. If you think of it like that, the rest of the problems evaporate. In particlar, look at Assembly.Load(byte[]) for loading an Assembly from binary. To write it as binary, use CompileAssemblyFromSource and look at the result's PathToAssembly - then File.ReadAllBytes(path) to obtain the binary from the file.

这篇关于如何在一个字节数组之间对一个装配对象进行序列化/反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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