coderushforroslyn-115537-coding-assistance-code-providers-implementation-providers-implement-iserializable.md
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.
Available when the caret is in a type declaration.
Place the caret in a type declaration (for example the Person class). The type should contain at least one field or auto-implemented property.
Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
Select Implement ISerializable from the menu.
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);
}
}
}
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