using System.Runtime.Serialization.Formatters.Binary;if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(this.saveFileDialog1.OpenFile(), object);
}
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(this.saveFileDialog1.OpenFile(), object);
}
然后把你的类标志为可序列化阿
然后用你的类不就行了
{
Hashtable infoTable;
public ObjectInfo()
{
infoTable = new Hashtable();
}
#region ISerializable 成员 public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
// TODO: 添加 ObjectInfo.GetObjectData 实现
} #endregion public void SaveInfo(object sourceObject)
{
Type type = sourceObject.GetType();
System.Reflection.PropertyInfo[] propertyInfors = type.GetProperties();
for(int i=0 ; i< propertyInfors.Length; i++)
{
System.Reflection.PropertyInfo propertyInfor = propertyInfors[i];
infoTable.Add(propertyInfor.Name,propertyInfor.GetValue(sourceObject,null));
}
} public void GetInfo(object sourceObject)
{
Type type = sourceObject.GetType();
System.Reflection.PropertyInfo[] propertyInfors = type.GetProperties();
for(int i=0 ; i< propertyInfors.Length; i++)
{
System.Reflection.PropertyInfo propertyInfor = propertyInfors[i];
if(infoTable.ContainsKey(propertyInfor.Name))
{
propertyInfor.SetValue(sourceObject,infoTable[propertyInfor.Name],null);
}
}
}
}
Binary Serialization和XML Serialization要求不一样的。