Serialize a Form along with all its data in C#

て烟熏妆下的殇ゞ 提交于 2019-12-11 18:32:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!