Implement ISerializable

Purpose

Using this Code Provider, you can easily add the implementation of the ISerializable interface to any class containing at least one field or property. This Code Provider adds the ISerializable implementation along with all required methods (GetObjectData() method and the deserialization constructor) to the class.

Availability

Available when the caret is on a class declaration.

Usage

  1. Place the caret on a class declaration. The class must contain at least one field or property.

    Note

    The blinking cursor shows the caret's position at which the Code Provider is available.

    public class Person {
        private string name_value;
        private int ID_value;
    
        public Person() { }
    
        public string Name {
            get { return name_value; }
            set { name_value = value; }
        }
    
        public int IdNumber {
            get { return ID_value; }
            set { ID_value = value; }
        }
    }
    
  2. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.
  3. Select Implement ISerializable from the menu.

After execution, the Code Provider adds the implementation of the ISerializable interface. It also adds the corresponding overloads of methods with their common implementation.

[Serializable]
public class Person : ISerializable {
    private string name_value;
    private int ID_value;

    public Person() { }

    public string Name {
        get { return name_value; }
        set { name_value = value; }
    }

    public int IdNumber {
        get { return ID_value; }
        set { ID_value = value; }
    }

    protected Person(SerializationInfo info, StreamingContext context) {
        if (info == null)
            return;
        name_value = info.GetString("name_value");
        ID_value = info.GetInt32("ID_value");
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context) {
        if (info == null)
            return;
        info.AddValue("name_value", name_value);
        info.AddValue("ID_value", ID_value);
    }
}
Note

If the source file does not contain reference to the System.Runtime.Serialization namespace, the corresponding reference will be added.

See Also