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);
    fs = new FileStream(path, System.IO.FileMode.Create);
  BinaryFormatter bf = new BinaryFormatter();
    BinaryFormatter bf = new BinaryFormatter();
  bf.Serialize(fs, gameUnit);
    bf.Serialize(fs, gameUnit);
}
  }
catch (Exception e)
  catch (Exception e)
{
  {
  // TODO: Fehlerbehandlung hier
    // TODO: Fehlerbehandlung hier
}
  }
finally
  finally
{
  {
  // Immer den Stream schliessen
    // Immer den Stream schliessen
  if (fs != null)
    if (fs != null)
    {
      {
      fs.Close();
        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);
    fs = new FileStream(path, FileMode.Open);
  BinaryFormatter bf = new BinaryFormatter();
    BinaryFormatter bf = new BinaryFormatter();
               
                 
  return (GameUnit)bf.Deserialize(fs);  
    return (GameUnit)bf.Deserialize(fs);  
}
  }
catch (Exception e)
  catch (Exception e)
{
  {
  // TODO: Fehlerbehandlung hier
    // TODO: Fehlerbehandlung hier
  return null;
    return null;
}
  }
finally
  finally
{
  if (fs != null)
   {
   {
     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);
    fs = new FileStream(path, System.IO.FileMode.Create);
  XmlSerializer seri = new XmlSerializer(typeof(GameUnit));
    XmlSerializer seri = new XmlSerializer(typeof(GameUnit));
  seri.Serialize(fs, gameUnit);
    seri.Serialize(fs, gameUnit);
}
  }
catch (Exception e)
  catch (Exception e)
{
  {
  // TODO: Fehlerbehandlung hier
    // TODO: Fehlerbehandlung hier
}
  }
finally
  finally
{
  {
  // Immer den Stream schliessen
    // Immer den Stream schliessen
  if (fs != null)
    if (fs != null)
    {
      {
      fs.Close();
        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);
    fs = new FileStream(path, FileMode.Open);
  XmlSerializer seri = new XmlSerializer(typeof(GameUnit));                 
    XmlSerializer seri = new XmlSerializer(typeof(GameUnit));                 
  return (GameUnit)seri.Deserialize(fs);  
    return (GameUnit)seri.Deserialize(fs);  
}
  }
catch (Exception e)
  catch (Exception e)
{
  {
  // TODO: Fehlerbehandlung hier
    // TODO: Fehlerbehandlung hier
  return null;
    return null;
}
  }
finally
  finally
{
  if (fs != null)
   {
   {
     fs.Close();
     if (fs != null)
    {
      fs.Close();
    }
   }
   }
}
}
}
</source>
</source>