Serialisierung: Unterschied zwischen den Versionen
Aus Das Sopra Wiki
Jan (Diskussion | Beiträge) |
Jan (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
||
| Zeile 71: | Zeile 71: | ||
void SaveUnit(GameUnit gameUnit, String path) | void SaveUnit(GameUnit gameUnit, String path) | ||
{ | { | ||
FileStream fs = null; | FileStream fs = null; | ||
try | try | ||
{ | { | ||
fs = new FileStream(path, System.IO.FileMode.Create); | |||
BinaryFormatter bf = new BinaryFormatter(); | |||
bf.Serialize(fs, gameUnit); | |||
} | } | ||
catch (Exception e) | catch (Exception e) | ||
{ | { | ||
// TODO: Fehlerbehandlung hier | |||
} | } | ||
finally | finally | ||
{ | { | ||
// Immer den Stream schliessen | |||
if (fs != null) | |||
{ | |||
fs.Close(); | |||
} | |||
} | } | ||
} | } | ||
} | } | ||
</source> | </source> | ||
| Zeile 98: | Zeile 98: | ||
GameUnit LoadUnit(String path) | GameUnit LoadUnit(String path) | ||
{ | { | ||
FileStream fs = null; | FileStream fs = null; | ||
try | try | ||
{ | { | ||
fs = new FileStream(path, FileMode.Open); | |||
BinaryFormatter bf = new BinaryFormatter(); | |||
return (GameUnit)bf.Deserialize(fs); | |||
} | } | ||
catch (Exception e) | catch (Exception e) | ||
{ | { | ||
// TODO: Fehlerbehandlung hier | |||
return null; | |||
} | } | ||
finally | finally | ||
{ | { | ||
fs.Close(); | if (fs != null) | ||
{ | |||
fs.Close(); | |||
} | |||
} | } | ||
} | } | ||
</source> | </source> | ||
| Zeile 131: | Zeile 131: | ||
void SaveUnit(GameUnit gameUnit, String path) | void SaveUnit(GameUnit gameUnit, String path) | ||
{ | { | ||
FileStream fs = null; | FileStream fs = null; | ||
try | try | ||
{ | { | ||
fs = new FileStream(path, System.IO.FileMode.Create); | |||
XmlSerializer seri = new XmlSerializer(typeof(GameUnit)); | |||
seri.Serialize(fs, gameUnit); | |||
} | } | ||
catch (Exception e) | catch (Exception e) | ||
{ | { | ||
// TODO: Fehlerbehandlung hier | |||
} | } | ||
finally | finally | ||
{ | { | ||
// Immer den Stream schliessen | |||
if (fs != null) | |||
{ | |||
fs.Close(); | |||
} | |||
} | } | ||
} | } | ||
} | } | ||
</source> | </source> | ||
| Zeile 157: | Zeile 157: | ||
GameUnit LoadUnit(String path) | GameUnit LoadUnit(String path) | ||
{ | { | ||
FileStream fs = null; | FileStream fs = null; | ||
try | try | ||
{ | { | ||
fs = new FileStream(path, FileMode.Open); | |||
XmlSerializer seri = new XmlSerializer(typeof(GameUnit)); | |||
return (GameUnit)seri.Deserialize(fs); | |||
} | } | ||
catch (Exception e) | catch (Exception e) | ||
{ | { | ||
// TODO: Fehlerbehandlung hier | |||
return null; | |||
} | } | ||
finally | finally | ||
{ | { | ||
fs.Close(); | if (fs != null) | ||
{ | |||
fs.Close(); | |||
} | |||
} | } | ||
} | } | ||
</source> | </source> | ||
