looking for a brokerage account or IRA... click here Add To Favorites
return to index 

C# Serialization

Serialization (known as pickling in python) is an easy way to convert an object to a binary representation that can then be e.g. written to disk or sent over a wire.

It''s useful e.g. for easy saving of settings to a file.

You can serialize your own classes if you mark them with [Serializable] attribute. This serializes all members of a class, except those marked as [NonSerialized].

.NET offers 2 serializers: binary, SOAP, XML. The difference between binary and SOAP is:

  • binary is more efficient (time and memory used)
  • binary is completely human-unreadable. SOAP isn''t much better.
  • XML is slightly different:

  • it lives in System.Xml.Serialization
  • it uses [XmlIgnore] instead of [NonSerialized] and ignores [Serializable]
  • it doesn''t serialize private class members
  • An example of serialization/deserialization to a file:

    using System.IO;
    using System.Diagnostics;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters;
    using System.Runtime.Serialization.Formatters.Binary;
    
    [Serializable]
    public class MySettings {
        public int screenDx;
        public ArrayList recentlyOpenedFiles;
        [NonSerialized]public string dummy;
    }
    
    public class Settings {
        const int VERSION = 1;
        static void Save(MySettings settings, string fileName) {
                Stream stream = null;
                try {
                    IFormatter formatter = new BinaryFormatter();
                    stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
                    formatter.Serialize(stream, VERSION);
                    formatter.Serialize(stream, settings);
                } catch {
                    // do nothing, just ignore any possible errors
                } finally {
                    if (null != stream)
                        stream.Close();
                }
        }
        
        static MySettings Load(string fileNmae) {
            Stream stream = null;
            MySettings settings = null;
            try {
                IFormatter formatter = new BinaryFormatter();
                stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
                int version = (ArrayList)formatter.Deserialize(stream);
                Debug.Assert(version == VERSION);
                settings = (MySettings)formatter.Deserialize(stream);
            } catch {
                // do nothing, just ignore any possible errors
            } finally {
                if (null != stream)
                    stream.Close();
            }
            return settings;
        }
    }
    
    

    Additional Interesting Articles

    Block File Leechers Using PHP
    PHP Cookie And Authentication
    SQL Create Procedure
    Stepwise Linear Regression in t-SQL
    SQL Server Reporting Services- Custom Chart Colors
    SpamAssassin Rules

    ©2008 AndrewKimball.com