对 ISerializable 类型调用基类方法
更新:2007 年 11 月
TypeName |
CallBaseClassMethodsOnISerializableTypes |
CheckId |
CA2236 |
类别 |
Microsoft.Usage |
是否重大更改 |
否 |
原因
从另一个类型派生的类型实现 System.Runtime.Serialization.ISerializable 接口,并且满足下列条件之一:
该类型实现序列化构造函数(即带 System.Runtime.Serialization.SerializationInfo、System.Runtime.Serialization.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);
}
}
}