Udostępnij za pośrednictwem


CA2235: Oznaczyć wszystkie pola nie można serializować

TypeName

MarkAllNonSerializableFields

CheckId

CA2235

Kategoria

Microsoft.Usage

Zmiana kluczowa

Niekluczowa

Przyczyna

Pole wystąpienia typu, który nie może być serializowany, jest zadeklarowane w typie, który może być serializowany.

Opis reguły

Typ możliwy do serializacji jest oznaczony atrybutem System.SerializableAttribute.Gdy typ jest serializowany, zgłaszany jest wyjątek System.Runtime.Serialization.SerializationException, jeśli typ zawiera pole wystąpienia typu, który nie może być serializowane.

Jak naprawić naruszenia

Aby naprawić naruszenie tej reguły, zastosuj do atrybut System.NonSerializedAttribute do pola, które nie może być serializowane.

Kiedy pominąć ostrzeżenia

Pomiń ostrzeżenie dotyczące tej reguły, tylko jeśli zadeklarowany jest typ System.Runtime.Serialization.ISerializationSurrogate, który umożliwia wystąpieniu pola serializację i deserializację.

Przykład

Poniższy przykład pokazuje typ, który narusza regułę oraz typ, który spełnia regułę.

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
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"); 
      }
   }
}

Powiązane reguły

CA2236: Wywołanie metody klasy bazowej typy ISerializable

CA2240: Wdrożenie ISerializable poprawnie

CA2229: Konstruktory serializacji wdrożenie

CA2238: Prawidłowo zaimplementować Serializacja metod

CA2237: Typy ISerializable znak z SerializableAttribute

CA2239: Zapewnić deserialization metody pola opcjonalne

CA2120: Konstruktory serializacji bezpiecznego