问题
I'm guessing that the best way to do this is by using XML. Here's the issue, tho:
Hero hero;
PictureBox heroPB;
Dictionary <string,Point> pokedex;
Boss boss;
PictureBox bossPB;
Attack attack;
PictureBox attackPB;
HPMeter bossHP;
PictureBox bossHPPB;
PPMeter heroPP;
PictureBox heroPPPB;
System.Timers.Timer animateAttack;
System.Timers.Timer animateBoss;
System.Timers.Timer checkHit;
System.Timers.Timer winFade;
Thread tr;
Rectangle bossRect;
Rectangle attackRect;
Panel whiteout;
int heroState = 2;
int stepRate = 5;
int attackDirection;
int attackLoop = 0;
int contadorPaso = 0;
int contadorPasoBoss = 0;
int bossTop, bossLeft;
int bossState = 6;
int bossHealth = 0;
int bossHPCap = 0;
int opa = 0;
int battlesWon = 0;
int mainBossCounter = 0;
int ppleft = 0;
bool paso = false;
bool inStadium = false;
bool fading = true;
bool fightingMainBoss = false;
bool fainted;
string currentPokemon = "";
Rectangle[] frames;
Rectangle[] entrances;
PictureBox[] boundaries;
Random r;
Random eth;
public delegate void BeginFade();
BeginFade fade;
I have more than a few objects/primitives which are constantly changing. Is there an efficient way to serialize the whole thing and load it the next time the program runs?
回答1:
There's really no simple method to save this data. I personally don't think XML is the best route. It would be nice for the objects and bad for everything else. I'd be more inclined to write it as JSON with a "misc" object that has all the lone primitives. I'd actually be most inclined to make my own config file reader/writer and write something more like a csv. Most of the config files at my place of work have at least that much data. We use key=value
for all the primitives. Each object would have it's own section where their primitives are listed in the key=value
format and the lists would be key=value1,value2,value3
. I personally think this is a fairly simple way to do things and writing the reader/writer class does not take very long. In fact, making a simple reader/writer from scratch would probably be faster than building one on top of a JSON or XML serialization class. I don't really see either of those formats benefiting you much here.
来源:https://stackoverflow.com/questions/13870879/serialize-a-form-along-with-all-its-data-in-c-sharp