CA2235 : Marquez tous les champs non sérialisésCA2235: Mark all non-serializable fields

TypeNameTypeName MarkAllNonSerializableFieldsMarkAllNonSerializableFields
CheckIdCheckId CA2235CA2235
CategoryCategory Microsoft.UsageMicrosoft.Usage
Modification avec ruptureBreaking Change Sans ruptureNon Breaking

CauseCause

Un champ d'instance d'un type non sérialisable est déclaré dans un type sérialisable.An instance field of a type that is not serializable is declared in a type that is serializable.

Description de la règleRule description

Un type sérialisable est celui qui est marqué avec le System.SerializableAttribute attribut.A serializable type is one that is marked with the System.SerializableAttribute attribute. Lorsque le type est sérialisé, une System.Runtime.Serialization.SerializationException exception est levée si un type contient un champ d’instance d’un type qui n’est pas sérialisable.When the type is serialized, a System.Runtime.Serialization.SerializationException exception is thrown if a type contains an instance field of a type that is not serializable.

Comment corriger les violationsHow to fix violations

Pour corriger une violation de cette règle, appliquez le System.NonSerializedAttribute attribut au champ qui n’est pas sérialisable.To fix a violation of this rule, apply the System.NonSerializedAttribute attribute to the field that is not serializable.

Quand supprimer les avertissementsWhen to suppress warnings

Supprimez uniquement un avertissement de cette règle si un System.Runtime.Serialization.ISerializationSurrogate type est déclaré qui permet aux instances du champ à être sérialisées et désérialisées.Only suppress a warning from this rule if a System.Runtime.Serialization.ISerializationSurrogate type is declared that allows instances of the field to be serialized and deserialized.

ExempleExample

L’exemple suivant montre un type qui viole la règle et un type qui satisfait la règle.The following example shows a type that violates the rule and a type that satisfies the rule.

using System;
using System.Runtime.Serialization;

namespace UsageLibrary
{
   public class Mouse
   {
      int buttons;
      string scanTypeValue;

      public int NumberOfButtons
      {
         get { return buttons; }
      }

      public string ScanType
      {
         get { return scanTypeValue; }
      }

      public Mouse(int numberOfButtons, string scanType)
      {
         buttons = numberOfButtons;
         scanTypeValue = scanType;
      }
   }

   [SerializableAttribute]
   public class InputDevices1
   {
      // Violates MarkAllNonSerializableFields.
      Mouse opticalMouse;

      public InputDevices1()
      {
         opticalMouse = new Mouse(5, "optical"); 
      }
   }

   [SerializableAttribute]
   public class InputDevices2
   {
      // Satisfies MarkAllNonSerializableFields.
      [NonSerializedAttribute]
      Mouse opticalMouse;

      public InputDevices2()
      {
         opticalMouse = new Mouse(5, "optical"); 
      }
   }
}
Imports System
Imports System.Runtime.Serialization

Namespace UsageLibrary

   Public Class Mouse
   
      Dim buttons As Integer
      Dim scanTypeValue As String

      ReadOnly Property NumberOfButtons As Integer
         Get
            Return buttons
         End Get
      End Property

      ReadOnly Property ScanType As String
         Get
            Return scanTypeValue
         End Get
      End Property

      Sub New(numberOfButtons As Integer, scanType As String)
         buttons = numberOfButtons
         scanTypeValue = scanType
      End Sub

   End Class

   <SerializableAttribute> _ 
   Public Class InputDevices1
   
      ' Violates MarkAllNonSerializableFields.
      Dim opticalMouse As Mouse 

      Sub New()
         opticalMouse = New Mouse(5, "optical") 
      End Sub

   End Class

   <SerializableAttribute> _ 
   Public Class InputDevices2
   
      ' Satisfies MarkAllNonSerializableFields.
      <NonSerializedAttribute> _ 
      Dim opticalMouse As Mouse 

      Sub New()
         opticalMouse = New Mouse(5, "optical") 
      End Sub

   End Class

End Namespace

CA2236 : Appelez les méthodes de la classe de base sur les types ISerializableCA2236: Call base class methods on ISerializable types

CA2240 : Implémentez ISerializable correctementCA2240: Implement ISerializable correctly

CA2229 : Implémentez des constructeurs de sérialisationCA2229: Implement serialization constructors

CA2238 : Implémentez les méthodes de sérialisation correctementCA2238: Implement serialization methods correctly

CA2237 : Marquez les types ISerializable avec SerializableAttributeCA2237: Mark ISerializable types with SerializableAttribute

CA2239 : Spécifiez des méthodes de désérialisation pour les champs facultatifsCA2239: Provide deserialization methods for optional fields

CA2120 : Sécurisez les constructeurs de sérialisationCA2120: Secure serialization constructors