Implement ISerializable

  • 3 minutes to read

Purpose

The Implement ISerializable code provider adds the implementation of the ISerializable interface to a type which contains at least one field or auto-implemented property.

This code provider creates a virtual GetObjectData() method and adds a Throw ArgumentNullException Contract in both the serialization constructor and ISerializable.GetObjectData method. This code provider supports the NonSerialized attribute.

Availability

Available when the caret is in a type declaration.

How to Use

  1. Place the caret in a type declaration (for example the Person class). The type should contain at least one field or auto-implemented property.

    using System;
    
    namespace ImplementISerializable {
        public class Person {
            private int ID_value;
            private string name_value;
    
            [field: NonSerialized]
            internal bool IsActive { get; set; }
    
            public int IdNumber {
               get { return ID_value; }
               set { ID_value = value; }
            }
    
            public string Name {
                get { return name_value; }
                set { name_value = value; }
            }
        }
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
  3. Select Implement ISerializable from the menu.

    Implement

After execution, the code provider adds the implementation of the ISerializable interface.

using System;
using System.Runtime.Serialization;

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

        [field: NonSerialized]
        internal bool IsActive { get; set; }

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

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

        protected Person(SerializationInfo info, StreamingContext context) {
            if (info == null) {
                throw new ArgumentNullException(nameof(info), $"{nameof(info)} is null.");
            }

            ID_value = info.GetInt32(nameof(IdNumber));
            name_value = info.GetString(nameof(Name));
        }

        protected virtual void GetObjectData(SerializationInfo info, StreamingContext context) {
            info.AddValue(nameof(IdNumber), ID_value);
            info.AddValue(nameof(Name), name_value);
        }

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
            if (info == null) {
                throw new ArgumentNullException(nameof(info), $"{nameof(info)} is null.");
            }

            GetObjectData(info, context);
        }
    }
}

Note

If the source file does not contain reference to the System.Runtime.Serialization namespace, CodeRush adds the corresponding reference.

See Also