how to serialize/deserialize an assembly object to and from a byte array

后端 未结 4 838
予麋鹿
予麋鹿 2020-12-18 08:59

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 da

4条回答
  •  太阳男子
    2020-12-18 09:46

    System.Reflection.Assembly is ISerializable and can simply be serialized like so:

    Assembly asm = Assembly.GetExecutingAssembly();
    BinaryFormatter formatter = new BinaryFormatter();
    MemoryStream stream = new MemoryStream();
    formatter.Serialize(stream, asm);
    

    and deserialization is just as simple but call BinaryFormatter.Deserialize instead.

提交回复
热议问题