I follow this tutorial online exactly but somehow it's giving me errors. Saying there is no object map or something.
I have the following static object that I want to serialize:
[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);
}
}
}
And here is the Read/Write functions:
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();
}
ACTUAL ERROR MSG: No map for Object '822476800'. This occur on this line:
bf.Deserialize(fileStream);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…