Mark all non-serializable fields
Mark all non-serializable fields
Microsoft docsDescription
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 CA2235When 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.