Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Implement serialization methods correctly
Collapse the table of content
Expand the table of content

Implement serialization methods correctly

TypeName

ImplementSerializationMethodsCorrectly

CheckId

CA2238

Category

Microsoft.Usage

Breaking Change

Breaking,NonBreaking

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:

Serialization event handlers take a single parameter of type System.Runtime.Serialization.StreamingContext, return void, and have private visibility.

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

Do not exclude a warning from this rule.

The following example shows correctly declared serialization event handlers.

using System;
using System.Runtime.Serialization;

namespace UsageLibrary
{
   [SerializableAttribute]
   public class SerializationEventHandlers
   {
      [OnSerializingAttribute]
      void OnSerializing(StreamingContext context) {}

      [OnSerializedAttribute]
      void OnSerialized(StreamingContext context) {}

      [OnDeserializingAttribute]
      void OnDeserializing(StreamingContext context) {}

      [OnDeserializedAttribute]
      void OnDeserialized(StreamingContext context) {}
   }
}

Community Additions

ADD
Show:
© 2015 Microsoft