Listing 1

public class UserPreferences
{
	private Rectangle windowBounds = new Rectangle(20, 20, 624, 596);

	public Rectangle WindowBounds
	{
		get { return windowBounds; }
		set { windowBounds = value; }
	}

	// factory function:
	public static UserPreferences LoadFromFile(string pathName)
	{
		UserPreferences rVal = new UserPreferences();
		FileStream fsStorage = null;
		XmlReader reader = null;
		try
		{
			fsStorage = new FileStream(pathName, FileMode.Open);
			reader = new XmlTextReader
			(fsStorage);
			XmlSerializer serializer = new XmlSerializer(typeof(UserPreferences));

			// Use the Deserialize method to restore the object's state.
			rVal = serializer.Deserialize(reader) as UserPreferences;
		}
		catch (IOException)
		{
			//No File or Can't open File.
			// Return defaults.
		}
		catch (System.InvalidOperationException)
		{
			// The file was not an XML file. (usually.)
			// return reasonable defaults.
		}
		finally
		{
			if (reader != null)
				reader.Close();
			if (fsStorage != null)
				fsStorage.Close();
		}
		return rVal;
	}

	// save:
	public void SaveToFile(string pathName)
	{
		using (TextWriter writer = new StreamWriter(pathName))
		{
			XmlSerializer serializer = 
			new XmlSerializer(typeof(UserPreferences));
			serializer.Serialize(writer, this);
		}
	}
}

Listing 2

public class AutoSave<T> where T : class, new()
{
	private T theStorage = new T();
	public static implicit operator T(AutoSave<T> wrapper)
	{
		return wrapper.theStorage;
	}

	public static AutoSave<T> LoadFromFile(string pathName)
	{
		AutoSave<T> rVal = new AutoSave<T>();
		FileStream fsStorage = null;
		XmlReader reader = null;
		try
		{
			fsStorage = new FileStream(pathName, FileMode.Open);
			reader = new XmlTextReader(fsStorage);
			XmlSerializer serializer = new XmlSerializer(typeof(T));
			// Use the Deserialize method to restore the object's state.
			rVal.theStorage = serializer.Deserialize(reader) as T;
		}
		catch (IOException)
		{
			//No FIle or Can't open File.
			// Return defaults.
		}
		catch (System.InvalidOperationException)
		{
			// The file was not an XML file. (usually.)
			// return reasonable defaults.
		}
		finally
		{
			if (reader != null)
				reader.Close();
			if (fsStorage != null)
				fsStorage.Close();
		}
		return rVal;
	}

	// save:
	public void SaveToFile(string pathName)
	{
		using (TextWriter writer = new StreamWriter(pathName))
		{
			XmlSerializer serializer = new XmlSerializer(typeof(T));
			serializer.Serialize(writer, this.theStorage);
		}
	}

	public T Value
	{
		get { return theStorage; }
	}
}

Listing 3

public class WorkingImages
{
	private List<CollageImage> listOfImages = new List<CollageImage>();

	public IList<CollageImage> TheCollage
	{
		get { return listOfImages; }
	}

	[XmlArray("TheCollage"),
	 XmlArrayItem(typeof(CollageImage) )]
	public IList SerializedImages
	{
		get { return listOfImages; }
	}
}

Listing 4

public class ImageProxy
{
	public delegate Image proxyLoader(string imagePath);

	private readonly string imagePath;
	private Image theImage;
	private readonly proxyLoader theBuilder;

	public ImageProxy(string path, proxyLoader factory)
	{
		imagePath = path;
		theBuilder = factory;
	}

	public Image Value
	{
		get
		{
			if (( theImage == null) && (theBuilder != null ))
				theImage = theBuilder(imagePath);
			return theImage;
		}
	}
}

Listing 5

public class Proxy<T, V> where T : class
{
	public delegate T proxyLoader(V imagePath);

	private readonly V targetParms;
	private T target;
	private readonly proxyLoader theBuilder;

	public Proxy(V parms, proxyLoader factory)
	{
		targetParms = parms;
		theBuilder = factory;
	}

	public T Value
	{
		get
		{
			if ((target == null) && (theBuilder != null))
				target = theBuilder(targetParms);
			return target;
		}
	}
}