All rules
CA2229Usage Enabled by default: No

Implement serialization constructors

Implement serialization constructors

Microsoft docs

Description

This rule is relevant for types that support custom serialization. A type supports custom serialization if it implements the System.Runtime.Serialization.ISerializable interface. The serialization constructor is required to deserialize, or recreate, objects that have been serialized using the System.Runtime.Serialization.ISerializable.GetObjectData method.

Cause

The type implements the System.Runtime.Serialization.ISerializable interface, is not a delegate or interface, and one of the following conditions is true:

  • The type does not have a constructor that takes a System.Runtime.Serialization.SerializationInfo object and a System.Runtime.Serialization.StreamingContext object (the signature of the serialization constructor).
  • The type is unsealed and the access modifier for its serialization constructor is not protected (family).
  • The type is sealed and the access modifier for its serialization constructor is not private.

How to fix violations

To fix a violation of this rule, implement the serialization constructor. For a sealed class, make the constructor private; otherwise, make it protected.

When to suppress

Do not suppress a violation of the rule. The type will not be deserializable, and will not function in many scenarios.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0