CA2236:对 ISerializable 类型调用基类方法

类型名

CallBaseClassMethodsOnISerializableTypes

CheckId

CA2236

类别

Microsoft.Usage

是否重大更改

原因

从另一个类型派生的类型实现 System.Runtime.Serialization.ISerializable 接口,并且满足下列条件之一:

规则说明

在自定义序列化过程中,某类型实现 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:以 SerializableAttribute 标记 ISerializable 类型

CA2239:为可选字段提供反序列化方法

CA2120:保护序列化构造函数