ISerializable

To gain even more control over the serialization process, you can implement the ISerializable interface and a special constructor:

[SerializableAttribute]
class MyData : ISerializable {
  string s = "Wahoo!";
  int n = 6;

  public string String {
    get { return s; }
    set { value = s; n = s.Length; }
  }

  public int Length {
    get { return n; }
  }

  public MyData() {}

  #region Implementation of ISerializable
					public MyData(
					SerializationInfo info, StreamingContext context) {
					// Get value from name/value pairs
					s = info.GetString("MyString");

    // Cache the string's length
    n = s.Length;
  }
					public void GetObjectData(
					SerializationInfo info, StreamingContext context) {
					// Add value to name/value pairs
					info.AddValue("MyString", s);
					}
					#endregion
}

Implementing ...

Get Windows Forms Programming in C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.