Serialization event handlers that don’t have the correct signature will simply not be called, thus bypassing any attempts to augment the automated
de/serialization.
This rule raises issue when a method marked with one of the following attributes is Public
, Shared
, is a
Function
not a Sub
, has type parameters, or does not have a single parameter of type
System.Runtime.Serialization.StreamingContext
:
-
System.Runtime.Serialization.OnSerializingAttribute
-
System.Runtime.Serialization.OnSerializedAttribute
-
System.Runtime.Serialization.OnDeserializingAttribute
-
System.Runtime.Serialization.OnDeserializedAttribute
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