Skip to content

Latest commit

 

History

History
83 lines (53 loc) · 2.86 KB

ca2238.md

File metadata and controls

83 lines (53 loc) · 2.86 KB
title description ms.date ms.topic f1_keywords helpviewer_keywords author ms.author manager ms.subservice dev_langs
CA2238: Implement serialization methods correctly
A method that handles a serialization event does not have the correct signature, return type, or visibility.
11/04/2016
reference
ImplementSerializationMethodsCorrectly
CA2238
ImplementSerializationMethodsCorrectly
CA2238
mikejo5000
mikejo
mijacobs
code-analysis
CSharp
VB

CA2238: Implement serialization methods correctly

Item Value
RuleId CA2238
Category Microsoft.Usage
Breaking change Breaking - If the method is visible outside the assembly.

Non-breaking - If the method is not visible outside the assembly.

Cause

A method that handles a serialization event does not have the correct signature, return type, or visibility.

Rule description

A method is designated a serialization event handler by applying one of the following serialization event attributes:

  • xref:System.Runtime.Serialization.OnSerializingAttribute?displayProperty=fullName

  • xref:System.Runtime.Serialization.OnSerializedAttribute?displayProperty=fullName

  • xref:System.Runtime.Serialization.OnDeserializingAttribute?displayProperty=fullName

  • xref:System.Runtime.Serialization.OnDeserializedAttribute?displayProperty=fullName

    Serialization event handlers take a single parameter of type xref:System.Runtime.Serialization.StreamingContext?displayProperty=fullName, return void, and have private visibility.

How to fix violations

To fix a violation of this rule, correct the signature, return type, or visibility of the serialization event handler.

When to suppress warnings

Do not suppress a warning from this rule.

Example

The following example shows correctly declared serialization event handlers.

:::code language="csharp" source="../snippets/csharp/VS_Snippets_CodeAnalysis/FxCop.Usage.SerializationEventHandlers/cs/FxCop.Usage.SerializationEventHandlers.cs" id="Snippet1":::

:::code language="vb" source="../snippets/visualbasic/VS_Snippets_CodeAnalysis/FxCop.Usage.SerializationEventHandlers/vb/FxCop.Usage.SerializationEventHandlers.vb" id="Snippet1":::

Related rules

CA2236: Call base class methods on ISerializable types

CA2240: Implement ISerializable correctly

CA2229: Implement serialization constructors

CA2235: Mark all non-serializable fields

CA2237: Mark ISerializable types with SerializableAttribute

CA2239: Provide deserialization methods for optional fields

CA2120: Secure serialization constructors