zoukankan      html  css  js  c++  java
  • 【C#】Switch datatype between object and byte[]

    This sample shows how to turn object to byte[], as well as turn byte[] to object.

    So,I can turn any types of object into byte[],which can be saved and transported properly.

    Attention!Attention!Attention!(Important things should be repeated three times:))

    1.Don't forget to using <System.IO; System.Runtime.Serialization.Formatters.Binary; System.Runtime.Serialization;> at the beginning of your program.

    2.When you need to transport an class/struct that defined by yourself, you'd better put the definition into a DLL file and using it in your program.

    3.Also,put a [Serializable] before the definition(if it is defined on your own).

     1 public static byte[] Object2Bytes(object obj)
     2 {
     3     IFormatter fmt = new BinaryFormatter();
     4     MemoryStream ms = new MemoryStream();
     5     fmt.Serialize(ms,obj);
     6     return ms.GetBuffer();
     7 }
     8 
     9 public static object Bytes2Object(byte[] bt)
    10 {
    11     IFormatter fmt = new BinaryFormatter();
    12     MemoryStream ms = new MemoryStream(bt);
    13     return (object)fmt.Deserialize(ms);
    14 }
  • 相关阅读:
    sdnu 1513 字符串翻转
    hdu-1559 最大子矩阵(二维树状数组模板题)
    hdu-1556 树状数组
    1049.饭盒
    1092.校门外的树
    1012.区间合并
    1054.数独
    1175.开心的金明 01背包
    空格(Space)的ASCII码值是:32
    js为lable和div赋值
  • 原文地址:https://www.cnblogs.com/Fefnir/p/5874557.html
Copyright © 2011-2022 走看看