## Why is this an issue?

Serialization event handlers that don’t have the correct signature will not be called, bypassing augmentations to automated serialization and deserialization events.

A method is designated a serialization event handler by applying one of the following serialization event attributes:

- `OnSerializingAttribute`
- `OnSerializedAttribute`
- `OnDeserializingAttribute`
- `OnDeserializedAttribute`

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

This rule raises an issue when any of these constraints are not respected.

## How to fix it

### Code examples

#### Noncompliant code example

    <Serializable>
    Public Class Foo
        <OnSerializing>
        Public Sub OnSerializing(ByVal context As StreamingContext) ' Noncompliant: should be private
        End Sub

        <OnSerialized>
        Private Function OnSerialized(ByVal context As StreamingContext) As Integer '  Noncompliant: should return void
        End Function

        <OnDeserializing>
        Private Sub OnDeserializing() ' Noncompliant: should have a single parameter of type StreamingContext
        End Sub

        <OnSerializing>
        Public Sub OnSerializing2(Of T)(ByVal context As StreamingContext) ' Noncompliant: should have no type parameters
        End Sub

        <OnDeserialized>
        Private Sub OnDeserialized(ByVal context As StreamingContext, ByVal str As String) ' Noncompliant: should have a single parameter of type StreamingContext
        End Sub
    End Class

#### Compliant solution

    <Serializable>
    Public Class Foo
        <OnSerializing>
        Private Sub OnSerializing(ByVal context As StreamingContext)
        End Sub

        <OnSerialized>
        Private Sub OnSerialized(ByVal context As StreamingContext)
        End Sub

        <OnDeserializing>
        Private Sub OnDeserializing(ByVal context As StreamingContext)
        End Sub

        <OnDeserialized>
        Private Sub OnDeserialized(ByVal context As StreamingContext)
        End Sub
    End Class

## Resources

### Documentation

- Microsoft Learn - CA2238: Implement serialization methods correctly