共用方式為


CA2236:必須呼叫 ISerializable 型別上的基底類別方法

型別名稱

CallBaseClassMethodsOnISerializableTypes

CheckId

CA2236

分類

Microsoft.Usage

中斷變更

不中斷

原因

型別衍生自實作 System.Runtime.Serialization.ISerializable 介面的型別,而且符合下列其中一個條件:

規則描述

在自訂序列化處理序 (Process) 中,型別會實作 GetObjectData 方法,序列化它的欄位及序列化建構函式,以便將欄位還原序列化。 如果型別是衍生自實作 ISerializable 介面的型別,則應會呼叫基底型別 GetObjectData 方法和序列化建構函式,以便將基底型別的欄位序列化/還原序列化。 否則,無法將該型別正確地序列化和還原序列化。 請注意,如果衍生型別 (Derived Type) 沒有加入任何新的欄位,則型別不需實作 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:必須以 SerializableAttribute 標記 ISerializable 型別

CA2239:必須為選擇性欄位提供還原序列化方法

CA2120:必須保護序列化建構函式