| 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: XML is slightly different:
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 ©2008 AndrewKimball.com |
|