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);
I figure out what the error is for those in future who might be banging their head wondering what went wrong. It’s actually really simple. A small typo actually. Too bad M$ have horrible error message that don’t really tell you where the error might have happened:
Simply replace this line:
With this:
And that’s it! Everything serialize/de-serialize just fine. You can’t possibly guess where it went wrong with the error msg: * No map for Object ‘822476800’*.
Note: In the last line, p.GetType should be value.GetType