Sdílet prostřednictvím


CA2236: Volejte metody třídy Base na typech ISerializable

TypeName

CallBaseClassMethodsOnISerializableTypes

CheckId

CA2236

Kategorie

Microsoft.Usage

Narušující změna

Nenarušující

Příčina

Typ je odvozen od typu, který implementuje rozhraní ISerializable a jedna z následujících podmínek je pravdivá:

Popis pravidla

Ve vlastním procesu serializace typ implementuje metodu GetObjectData pro serializaci svých polí a konstruktor serializace pro deserializaci těchto polí.Pokud je typ odvozen od typu, který implementuje rozhraní ISerializable, pak by metoda GetObjectData základního typu nebo konstruktor serializace měl být zavolán pro serializaci/deserializaci polí základního typu.Jinak typ nebude serializován a deserializován správně.Poznamenejme, že pokud odvozený typ nepřidává žádná nová pole, pak není nutné, aby tento typ implementoval metodu GetObjectData a ani konstruktor serializace nebo volal ekvivalenty základního typu.

Jak vyřešit porušení

Chcete-li napravit porušení tohoto pravidla, zavolejte metodu GetObjectData základního typu nebo konstruktor serializace z odpovídající metody nebo konstruktoru odvozeného typu.

Kdy potlačit upozornění

Nepotlačujte upozornění na toto pravidlo.

Příklad

Následující příklad ukazuje odvozený typ, který toto pravidlo splňuje pomocí volání konstruktoru serializace a metody GetObjectData své základní třídy.

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

Související pravidla

CA2240: Implementujte správně ISerializable

CA2229: Implementovat serializační konstruktory

CA2238: Implementujte správně metody serializace

CA2235: Označte všechna neserializovatelná pole

CA2237: Označte typy ISerializable pomocí SerializableAttribute

CA2239: Poskytujte metody deserializace pro nepovinné pole

CA2120: Zabezpečte serializační konstruktory