次の方法で共有


CA2236: ISerializable 型で基本クラス メソッドを呼び出します

TypeName

CallBaseClassMethodsOnISerializableTypes

CheckId

CA2236

分類

Microsoft.Usage

互換性に影響する変更点

なし

原因

ISerializable インターフェイスを実装している型から別の型が派生し、次の条件のいずれかに該当します。

  • 型でシリアル化コンストラクター (つまり、SerializationInfo を持つコンストラクター) である StreamingContext パラメーター シグネチャを実装していますが、基本型の同期化コンストラクターを呼び出していません。

  • 型で ISerializable.GetObjectData メソッドを実装していますが、基本型の GetObjectData メソッドを呼び出していません。

規則の説明

カスタムのシリアル化プロセスでは、型でフィールドをシリアル化するには GetObjectData メソッドを実装し、フィールドを逆シリアル化するにはシリアル化コンストラクターを実装します。その型が ISerializable インターフェイスを実装する型から派生する場合、基本型の GetObjectData メソッドとシリアル化コンストラクターを呼び出して、基本型のフィールドのシリアル化/逆シリアル化を行う必要があります。そうしないと、型のシリアル化/逆シリアル化が正しく行われません。派生型で新しいフィールドを追加しない場合、型で GetObjectData メソッドやシリアル化コンストラクターを実装したり、基本型で対応するメソッドやシリアル化コンストラクターを呼び出す必要はありません。

違反の修正方法

この規則違反を修正するには、基本型の GetObjectData メソッドまたはシリアル化コンストラクターを、対応する派生型のメソッドまたはコンストラクターから呼び出します。

警告を抑制する状況

この規則による警告は抑制しないでください。

使用例

次の例は、基本クラスのシリアル化コンストラクターと GetObjectData メソッドを呼び出すことでこの規則に適合する派生型を示します。

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

関連規則

CA2240: ISerializable を正しく実装します

CA2229: シリアル化コンストラクターを実装します

CA2238: シリアル化メソッドを正しく実装します

CA2235: すべてのシリアル化不可能なフィールドを設定します

CA2237: ISerializable 型を SerializableAttribute に設定します

CA2239: オプションのフィールドに逆シリアル化メソッドを指定します

CA2120: シリアル化コンストラクターをセキュリティで保護します