物件序列化 與反序列化

2021-09-26 20:10:16 字數 3661 閱讀 9932

1.序列化為二進位製流

view code?

usingsystem;

usingsystem.collections.generic;

usingsystem.text;

usingsystem.runtime.serialization.formatters.binary;

usingsystem.io;

namespacecommon.serialization.byte

memorystream ms =newmemorystream();

tryfinally

}

///

/// 二進位製流轉換為物件

///

/// 二進位製流快取

/// 反序列化後的物件

publicstaticobjectdeserialize(bytebuffer)

memorystream ms =newmemorystream(buffer);

tryfinally

}

}

}

2.序列化為xml

view code?

usingsystem;

usingsystem.text;

usingsystem.io;

namespacecommon.serialization.xml

memorystream ms =newmemorystream();

tryfinally

}

///

/// xml字串轉換為物件

///

/// xml字串

/// 要反序列化為的物件型別

/// 反序列化後的物件

publicstaticobjectdeserialize(stringxml, type targettype)

memorystream ms =newmemorystream(encoding.default.getbytes(xml));

tryfinally

}

}

}

3.序列化為json?

usingsystem;

usingsystem.text;

usingsystem.io;

usingsystem.runtime.serialization.json;

namespacecommon.serialization.json

memorystream ms =newmemorystream();

tryfinally

}

///

/// json字串轉換為物件

///

/// json字串

/// 要反序列化為的物件型別

/// 反序列化後的物件

publicstaticobjectdeserialize(stringjson, type targettype)

memorystream ms =newmemorystream(encoding.utf8.getbytes(json));

tryfinally

}

}

}

物件序列化與反序列化

using system using system.text using system.collections.generic using system.io using system.runtime.serialization.formatters.binary class serializabl...

物件序列化與反序列化

我們常常需要將一些程式當前資訊儲存在檔案中,如果明文儲存,那麼資訊不安全,使用物件序列化和反序列化可解決此問題 1 將需要儲存的資訊封裝成類,用serializable標記 serializable 次標記必須,以宣告此student類可執行序列化操作 class student public st...

物件序列化 反序列化

必須新增引用 using system.io using system.runtime.serialization using system.runtime.serialization.formatters.binary 方法 region 物件序列化 物件序列化 任意物件 字串 public st...