最近在做一個項目,項目有很多的對象狀態需要保存,以供其他的服務調用;我就想在做一個對象序列化的通用程序,因為二進制和XML各有優缺,就想做好后在使用的過程中跟據需求調用,真是沒想到一開始就碰了個大石頭,XML完沒有問題,但二進制反序列碰到了很頭疼的問題,在同進程里序列化反序列化都很正常,但如果退出程序在重新進去,在反序列化后得的對象里面的值都是空的,就是說跨進程后,反序化出來的對像就是一個空的;亂了幾天還是沒找到原因,這里我把測試的代碼貼出來,代碼非常簡單,請各位大蝦幫忙看看問題在那,小弟在這里謝過了!!!
這個是被序列化和反序列化的類很簡單就三個屬性
View Code
using System; namespace Serialization { [Serializable] public class book { private static string book_name; private static string book_isbn; [NonSerialized] public string book_price; public string bookName { set { book_name = value; } get { return book_name; } } public string bookIsbn { set { book_isbn = value; } get { return book_isbn; } } } }
這個是調用程序
View Code
/// <summary> /// 二進制序列化調用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void butSeriaBinary_Click(object sender, EventArgs e) { book bo = new book(); bo.bookIsbn = this.textBox1.Text; bo.bookName = this.textBox2.Text; bo.book_price = this.textBox3.Text; SerilizeAnobjectBinary(bo, @"C:\test.dat"); } /// <summary> /// 二進制反序列化調用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void butSeriaBinaryNo_Click(object sender, EventArgs e) { book bo = (book)DeserilizeAnObjectBinary(@"C:\test.dat"); this.textBox1.Text = bo.bookIsbn; this.textBox2.Text = bo.bookName; this.textBox3.Text = bo.book_price; } /// <summary> /// XML序列化調用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void butSeriaXml_Click(object sender, EventArgs e) { book bo = new book(); bo.bookIsbn = this.textBox1.Text; bo.bookName = this.textBox2.Text; bo.book_price = this.textBox3.Text; SerilizeAnObject(bo, @"C:\test.xml"); } /// <summary> /// XML反序列化調用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void butSeriaXmlNo_Click(object sender, EventArgs e) { book bo=(book)DeserilizeAnObject(typeof(book),@"c:\test.xml"); this.textBox1.Text = bo.bookIsbn; this.textBox2.Text = bo.bookName; this.textBox3.Text = bo.book_price; }
這個是XML的序列化和反序列化沒的問題,這里也貼出來參考下
View Code
/// <summary> /// XmlSerializer序列化 /// </summary> /// <param name="obj"></param> /// <param name="path"></param> private static void SerilizeAnObject(object obj, string path) { System.IO.FileStream stream = new FileStream(path, FileMode.Create); try { XmlSerializer serializer = new XmlSerializer(obj.GetType()); serializer.Serialize(stream, obj); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { stream.Close(); stream.Dispose(); } } /// <summary> /// XmlSerializer反序列化 /// </summary> /// <param name="type"></param> /// <param name="path"></param> /// <returns></returns> private static object DeserilizeAnObject(Type type, string path) { object obj = null; FileStream stream = new FileStream(path, FileMode.Open); try { XmlReader reader = new XmlTextReader(stream); XmlSerializer serializer =new XmlSerializer(type); obj = serializer.Deserialize(reader); } catch (Exception ex) { MessageBox.Show( ex.Message); } finally { stream.Close(); stream.Dispose(); } return obj; }
問題代碼來了,請各位高人一定幫幫忙
/// <summary>
/// BinaryFormatter二進制序列化
/// </summary>
/// <param name="obj"></param>
/// <param name="path"></param>
private static void SerilizeAnobjectBinary(object obj, string path)
{
FileStream stream = new FileStream(path, FileMode.Create);
try
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, obj);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
stream.Close();
stream.Dispose();
}
}
/// <summary>
/// BinaryFormatter二進制反序列化
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static object DeserilizeAnObjectBinary(string path)
{
object obj = null;
FileStream stream = new FileStream(path, FileMode.Open);
try
{
BinaryFormatter bf = new BinaryFormatter();
obj = bf.Deserialize(stream);
return obj;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return null;
}
finally
{
stream.Close();
stream.Dispose();
}
}
