c# - Binary Serialization, I think I almost have it working -
i've been following guide on binary serialization (this 1 here: http://www.codeproject.com/articles/1789/object-serialization-using-c) , think have working. when save file created, when try load, nothing loaded. feel i'm close getting working. advice appreciated. here's code:
save/load class
[serializable()] public class saveload : iserializable { public int gameday = date.gameday; public list<adventurer> adventurers = formmain.adventurermanager.adventurerlist; public saveload() { gameday = 0; adventurers = null; } public saveload(serializationinfo info, streamingcontext ctxt) { gameday = (int)info.getvalue("date", typeof(int)); adventurers = (list<adventurer>)info.getvalue("adventurers", typeof(list<adventurer>)); } public void getobjectdata(serializationinfo info, streamingcontext ctxt) { info.addvalue("date", gameday); info.addvalue("adventurers", adventurers); } }
save/load methods:
void btnsavegame_click(object sender, eventargs e) { saveload save = new saveload(); stream stream = file.open("savegame.osl", filemode.create); binaryformatter bformatter = new binaryformatter(); bformatter.serialize(stream, save); stream.close(); } void btnloadgame_click(object sender, eventargs e) { saveload load = new saveload(); stream stream = file.open("savegame.osl", filemode.open); binaryformatter bformatter = new binaryformatter(); load = (saveload)bformatter.deserialize(stream); stream.close(); date.calculatedate(); this.visible = false; ((formmain)(this.parentform)).controlmainscreen.visible = true; }
i think may have initialization timing issue.
try either moving initialization of gameday , adventurers constructor or rid of nulling them out in constructor. once did following, code pretty works:
public saveload() { //gameday = null; //adventurers = null; }
note had make sure adventurer class had serialization attribute.
here code serialization works me (i had create own adventurer class , replaced date string since couldn't figure out doing or coming from. populated adventurers list dummy data , commented out form stuff didn't have information on.
[serializable()] public class saveload : iserializable { public string gameday = null; public list<adventurer> adventurers = null; //formmain.adventurermanager.adventurerlist; public saveload() { gameday = "date"; adventurers = new list<adventurer>() { new adventurer { name = "a1", type = "t1" }, new adventurer { name = "a1", type = "t1" } }; ; } public saveload(serializationinfo info, streamingcontext ctxt) { gameday = (string)info.getvalue("date", typeof(string)); adventurers = (list<adventurer>)info.getvalue("adventurers", typeof(list<adventurer>)); } public void getobjectdata(serializationinfo info, streamingcontext ctxt) { info.addvalue("date", gameday); info.addvalue("adventurers", adventurers); } } [serializable()] public class adventurer { public string name { get; set; } public string type { get; set; } } private void btnloadgame_click(object sender, eventargs e) { saveload sl = new saveload(); stream stream = file.open("savegame.osl", filemode.open); binaryformatter bformatter = new binaryformatter(); sl = (saveload)bformatter.deserialize(stream); stream.close(); messagebox.show(sl.adventurers.count.tostring()); //date.calculatedate(); //this.visible = false; //((form1)(this.parentform)).controlmainscreen.visible = true; } private void btnsavegame_click(object sender, eventargs e) { saveload sl = new saveload(); stream stream = file.open("savegame.osl", filemode.create); binaryformatter bformatter = new binaryformatter(); bformatter.serialize(stream, sl); stream.close(); }
Comments
Post a Comment