Udostępnij za pośrednictwem


CA2236: Wywołanie metody klasy bazowej typy ISerializable

TypeName

CallBaseClassMethodsOnISerializableTypes

CheckId

CA2236

Kategoria

Microsoft.Usage

Zmiana kluczowa

Niekluczowa

Przyczyna

Typ dziedziczy po typie, który implementuje interfejs System.Runtime.Serialization.ISerializable i prawdziwy jest jeden z poniższych warunków:

Opis reguły

W procesie serializacji niestandardowej, typ implementuje metodę GetObjectData, aby serializować swoje pola, a konstruktor serializacji przeprowadza deserializację pól.Jeśli typ dziedziczy po typie, który implementuje interfejs ISerializable, metoda GetObjectData typu podstawowego i konstruktor serializacji powinny zostać wywołane, aby serializować/deserializować pola typu podstawowego.W przeciwnym razie, typ nie zostanie poprawnie serializowany i deserializowany.Zauważ, że jeśli typ pochodny nie dodaje żadnych nowych pól, to nie musi implementować metody GetObjectData, konstruktora serializacji ani wywoływać ich odpowiedników typu podstawowego.

Jak naprawić naruszenia

Aby naprawić naruszenie tej reguły, wywołaj metodę GetObjectData typu podstawowego lub konstruktor serializacji z odpowiadającej metody typu pochodnego lub konstruktora.

Kiedy pominąć ostrzeżenia

Nie należy pomijać ostrzeżenia dotyczącego tej reguły.

Przykład

Poniższy przykład pokazuje typu pochodny, który spełnia regułę, przez wywołanie konstruktora serializacji i metody GetObjectData klasy podstawowej.

Imports System
Imports System.Runtime.Serialization
Imports System.Security.Permissions

Namespace UsageLibrary

   <SerializableAttribute> _ 
   Public Class BaseType
      Implements ISerializable

      Dim baseValue As Integer

      Sub New()
         baseValue = 3
      End Sub

      Protected Sub New( _ 
         info As SerializationInfo, context As StreamingContext)

         baseValue = info.GetInt32("baseValue")

      End Sub

      <SecurityPermissionAttribute(SecurityAction.Demand, _ 
          SerializationFormatter := True)> _ 
      Overridable Sub GetObjectData( _ 
         info As SerializationInfo, context As StreamingContext) _ 
         Implements ISerializable.GetObjectData

         info.AddValue("baseValue", baseValue)

      End Sub

   End Class

   <SerializableAttribute> _ 
   Public Class DerivedType: Inherits BaseType

      Dim derivedValue As Integer

      Sub New()
         derivedValue = 4
      End Sub

      Protected Sub New( _ 
         info As SerializationInfo, context As StreamingContext)

         MyBase.New(info, context)      
         derivedValue = info.GetInt32("derivedValue")

      End Sub

      <SecurityPermissionAttribute(SecurityAction.Demand, _ 
          SerializationFormatter := True)> _ 
      Overrides Sub GetObjectData( _ 
         info As SerializationInfo, context As StreamingContext)

         info.AddValue("derivedValue", derivedValue)
         MyBase.GetObjectData(info, context)

      End Sub

   End Class

End Namespace
using System;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace UsageLibrary
{
   [SerializableAttribute]
   public class BaseType : ISerializable
   {
      int baseValue;

      public BaseType()
      {
         baseValue = 3;
      }

      protected BaseType(
         SerializationInfo info, StreamingContext context)
      {
         baseValue = info.GetInt32("baseValue");
      }

      [SecurityPermissionAttribute(SecurityAction.Demand, 
          SerializationFormatter = true)]
      public virtual void GetObjectData(
         SerializationInfo info, StreamingContext context)
      {
         info.AddValue("baseValue", baseValue);
      }
   }

   [SerializableAttribute]
   public class DerivedType : BaseType
   {
      int derivedValue;

      public DerivedType()
      {
         derivedValue = 4;
      }

      protected DerivedType(
         SerializationInfo info, StreamingContext context) : 
         base(info, context)
      {
         derivedValue = info.GetInt32("derivedValue");
      }

      [SecurityPermissionAttribute(SecurityAction.Demand, 
          SerializationFormatter = true)]
      public override void GetObjectData(
         SerializationInfo info, StreamingContext context)
      {
         info.AddValue("derivedValue", derivedValue);
         base.GetObjectData(info, context);
      }
   }
}

Powiązane reguły

CA2240: Wdrożenie ISerializable poprawnie

CA2229: Konstruktory serializacji wdrożenie

CA2238: Prawidłowo zaimplementować Serializacja metod

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

CA2237: Typy ISerializable znak z SerializableAttribute

CA2239: Zapewnić deserialization metody pola opcjonalne

CA2120: Konstruktory serializacji bezpiecznego