Example #1
0
        Agents.NervousSystem Deserialize(string openFile)
        {
            // Declare the hashtable reference.
            Agents.SerializableNS ns = null;

            // Open the file containing the data that you want to deserialize.
            FileStream fs = new FileStream(openFile, FileMode.Open);

            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                // Deserialize the hashtable from the file and
                // assign the reference to the local variable.
                ns = (Agents.SerializableNS)formatter.Deserialize(fs);
                return(new Agents.NervousSystem(ns));
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
                throw;
            }
            finally
            {
                fs.Close();
            }
        }
Example #2
0
        void Serialize(Agents.SerializableNS ns, string outFile)
        {
            FileStream fs = new FileStream(outFile, FileMode.Create);

            // Construct a BinaryFormatter and use it to serialize the data to the stream.
            BinaryFormatter formatter = new BinaryFormatter();

            try
            {
                formatter.Serialize(fs, ns);
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to serialize. Reason: " + e.Message);
                throw;
            }
            finally
            {
                fs.Close();
            }
        }