代码如下:
SerializableRegion RDStore = new SerializableRegion(this.Region);
Stream sw = File.Create(@"C:\FormMap.bin");
bf.Serialize(sw, RDStore); //到此,成功生成文件保存了序列化数据
sw.Seek(0, SeekOrigin.Begin);
RDStore = (SerializableRegion)bf.Deserialize(sw);//这一行出错,提示:未将对象引用设置到对象的实例
sw.Close();
注:SerializableRegion 是一个复杂类.
不知道要怎么解决?我是照着书做的,很奇怪会出错...新手,请多多指教.
SerializableRegion RDStore = new SerializableRegion(this.Region);
Stream sw = File.Create(@"C:\FormMap.bin");
bf.Serialize(sw, RDStore); //到此,成功生成文件保存了序列化数据
sw.Seek(0, SeekOrigin.Begin);
RDStore = (SerializableRegion)bf.Deserialize(sw);//这一行出错,提示:未将对象引用设置到对象的实例
sw.Close();
注:SerializableRegion 是一个复杂类.
不知道要怎么解决?我是照着书做的,很奇怪会出错...新手,请多多指教.
bf是怎么声明的?贴一个完整可调试的代码(包括:SerializableRegion声明)。
SerializableRegion RDStore = new SerializableRegion(this.Region);
Stream sw = File.Create(@"C:\FormMap.bin");
bf.Serialize(sw, RDStore); //到此,成功生成文件保存了序列化数据
sw.Seek(0, SeekOrigin.Begin);
RDStore = (SerializableRegion)bf.Deserialize(sw);//这一行出错,提示:未将对象引用设置到对象的实例
sw.Close();
注:SerializableRegion 是一个复杂类.
/**************************************************/
[Serializable]
public class SerializableRegion : ISerializable
{
private Region _region = null;
public SerializableRegion(Region region)
{
if (region == null)
throw new ArgumentNullException();
_region = region;
}
public SerializableRegion()
{
}
public Region Region
{
get { return this._region; }
}
public SerializableRegion(SerializationInfo info, StreamingContext context)
{
byte[] bytes = (byte[])info.GetValue("Data", typeof(byte[]));
ConstructorInfo constructInfor = typeof(RegionData).GetConstructor(new Type[] { typeof(byte[]) });
RegionData data = (RegionData)constructInfor.Invoke(new object[] { bytes });
this._region = new Region(data);
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
RegionData data = _region.GetRegionData();
info.AddValue("Data", data.Data);
}
}/************************************************************/
谢谢
//下面代码放在Form1_Load里;
BinaryFormatter bf = new BinaryFormatter();
SerializableRegion RDStore = new SerializableRegion(this.Region);
Stream sw = File.Create(@"C:\FormMap.bin");
bf.Serialize(sw, RDStore); //到此,成功生成文件保存了序列化数据
sw.Seek(0, SeekOrigin.Begin);
RDStore = (SerializableRegion)bf.Deserialize(sw);//这一行出错,提示:未将对象引用设置到对象的实例
sw.Close();
注:SerializableRegion 是一个复杂类.
/********************************************************/
//下面代码自己生成类
///SerializableRegion 类
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
using System.Drawing;
using System.Drawing.Drawing2D;namespace SerializRegion
{
[Serializable]
public class SerializableRegion : ISerializable
{
private Region _region = null;
public SerializableRegion(Region region)
{
if (region == null)
throw new ArgumentNullException();
_region = region;
}
public SerializableRegion()
{
}
public Region Region
{
get { return this._region; }
}
public SerializableRegion(SerializationInfo info, StreamingContext context)
{
byte[] bytes = (byte[])info.GetValue("Data", typeof(byte[]));
ConstructorInfo constructInfor = typeof(RegionData).GetConstructor(new Type[] { typeof(byte[]) });
RegionData data = (RegionData)constructInfor.Invoke(new object[] { bytes });
this._region = new Region(data);
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
RegionData data = _region.GetRegionData();
info.AddValue("Data", data.Data);
}
}
}
不知道我有没有说清楚?
捣腾一下:
protected SerializableRegion(SerializationInfo info, StreamingContext context)
{
if (info == null)
throw new System.ArgumentNullException("info");
Region r = new Region();
RegionData d = r.GetRegionData();
d.Data = (byte[])info.GetValue("Data", typeof(byte[]));
this._region = new Region(d);
r.Dispose();
}