Back to Devexpress

Implement ISerializable

coderushforroslyn-115537-coding-assistance-code-providers-implementation-providers-implement-iserializable.md

latest4.6 KB
Original Source

Implement ISerializable

  • Aug 19, 2021
  • 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.

  2. Press 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.

csharp
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);
        }
    }
}
vb
Imports System
Imports System.Runtime.Serialization

<Serializable>
Public Class Person
    Implements ISerializable

    Private name_value As String
    Private ID_value As Integer

    <NonSerialized()>
    Friend IsActive As Boolean

    Public Property IdNumber() As Integer
        Get
            Return ID_value
        End Get
        Set(ByVal value As Integer)
            ID_value = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return name_value
        End Get
        Set(ByVal value As String)
            name_value = value
        End Set
    End Property

    Protected Sub New(info As SerializationInfo, context As StreamingContext)
        If info Is Nothing Then
            Throw New ArgumentNullException(NameOf(info), $"{NameOf(info)} is null.")
        End If

        name_value = info.GetString(NameOf(name_value))
        ID_value = info.GetInt32(NameOf(ID_value))
    End Sub

    Protected Overridable Sub GetObjectData(info As SerializationInfo, context As StreamingContext) Implements ISerializable.GetObjectData
        If info Is Nothing Then
            Throw New ArgumentNullException(NameOf(info), $"{NameOf(info)} is null.")
        End If

        info.AddValue(NameOf(name_value), name_value)
        info.AddValue(NameOf(ID_value), ID_value)
    End Sub
End Class

Note

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

See Also

Implement IEquatable

Implement IComparable