最近在做一个项目,项目有很多的对象状态需要保存,以供其他的服务调用;我就想在做一个对象序列化的通用程序,因为二进制和XML各有优缺,就想做好后在使用的过程中跟据需求调用,真是没想到一开始就碰了个大石头,XML完没有问题,但二进制反序列碰到了很头疼的问题,在同进程里序列化反序列化都很正常,但如果退出程序在重新进去,在反序列化后得的对象里面的值都是空的,就是说跨进程后,反序化出来的对像就是一个空的;乱了几天还是没找到原因,这里我把测试的代码贴出来,代码非常简单,请各位大虾帮忙看看问题在那,小弟在这里谢过了!!!
这个是被序列化和反序列化的类很简单就三个属性
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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; } } } }
这个是调用程序
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/// <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的序列化和反序列化没的问题,这里也贴出来参考下
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/// <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(); } }