我完全按照本教程在线,但不知何故它给了我错误。说没有对象图什么的。
我有以下要序列化的静态对象:
[Serializable]
public class Settings : ISerializable
{
public static string server= "http://localhost/";
public static string username = "myname";
public static bool savePassword = true;
public static bool autoSync = true;
public static string password = "mypass";
public static string folderPath1= "c:/";
public static string folderPath2= "c:/";
public static string autoSyncDuration = "300";
public static string lastSyncTime = "???";
public Settings()
{ }
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
Type myTypeObj = Type.GetType("Settings");
foreach (FieldInfo p in myTypeObj.GetFields())
{
Object value = p.GetValue(null);
info.AddValue(p.Name, value, p.GetType());
}
}
public Settings(SerializationInfo info, StreamingContext context)
{
Type myTypeObj = Type.GetType("Settings");
FieldInfo p;
foreach (SerializationEntry e in info)
{
p = myTypeObj.GetField(e.Name);
p.SetValue(null, e.Value);
}
}
}
这是读/写功能:
private void writeSettings()
{
pcb_savingSettings.Visible = true;
FileStream fileStream = new FileStream(settingFile, FileMode.Create, FileAccess.Write, FileShare.None);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fileStream, new Settings());
fileStream.Close();
pcb_savingSettings.Visible = false;
}
private void readSettings()
{
if (!File.Exists(settingFile))
{
writeSettings();
}
FileStream fileStream = new FileStream(settingFile, FileMode.Open, FileAccess.Read, FileShare.None);
BinaryFormatter bf = new BinaryFormatter();
bf.Deserialize(fileStream);
fileStream.Close();
}
实际错误消息:对象“822476800”没有地图。这发生在这一行:
bf.Deserialize(fileStream);