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 |
|
|
mikejo5000 |
mikejo |
mijacobs |
code-analysis |
|
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. |
A method that handles a serialization event does not have the correct signature, return type, or visibility.
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 haveprivate
visibility.
To fix a violation of this rule, correct the signature, return type, or visibility of the serialization event handler.
Do not suppress a warning from this rule.
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":::
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