All rules
CA2235Usage Enabled by default: No

Mark all non-serializable fields

Mark all non-serializable fields

Microsoft docs

Description

A serializable type is one that is marked with the System.SerializableAttribute attribute. When the type is serialized, a System.Runtime.Serialization.SerializationException exception is thrown if the type contains an instance field of a type that's not serializable *and* doesn't implement the System.Runtime.Serialization.ISerializable interface. CA2235 does not fire for instance fields of types that implement System.Runtime.Serialization.ISerializable because they provide their own serialization logic.

Cause

An instance field of a type that is not serializable is declared in a type that is serializable.

How to fix violations

To fix a violation of this rule, apply the System.NonSerializedAttribute attribute to the field that is not serializable.

Example

#pragma warning disable CA2235
// The code that's violating the rule is on this line.
#pragma warning restore CA2235

When to suppress

Only suppress a warning from this rule if a System.Runtime.Serialization.ISerializationSurrogate type is declared that allows instances of the field to be serialized and deserialized.

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