2023-05-25 14:18:12 +02:00
== Why is this an issue?
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 an issue when a method marked with one of the following attributes is ``++public++``, ``++static++``, does not return ``++void++``, 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
[source,csharp]
----
[Serializable]
public class Foo
{
[OnSerializing]
public void OnSerializing(StreamingContext context) {} // Noncompliant should be private
[OnSerialized]
int OnSerialized(StreamingContext context) {} // Noncompliant should return void
[OnDeserializing]
void OnDeserializing() {} // Noncompliant should have a single parameter of type StreamingContext
[OnSerializing]
public void OnSerializing2<T>(StreamingContext context) {} // Noncompliant should have no type parameters
[OnDeserialized]
void OnDeserialized(StreamingContext context, string str) {} // Noncompliant should have a single parameter of type StreamingContext
}
----
=== Compliant solution
[source,csharp]
----
[Serializable]
public class Foo
{
[OnSerializing]
private void OnSerializing(StreamingContext context) {}
[OnSerialized]
private void OnSerialized(StreamingContext context) {}
[OnDeserializing]
private void OnDeserializing(StreamingContext context) {}
[OnDeserialized]
private void OnDeserialized(StreamingContext context) {}
}
----
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Make this method [non-public | non-static | return 'void' | have no type parameters | have a single parameter of type 'StreamingContext'].
2021-09-20 15:38:42 +02:00
include::../highlighting.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2023-06-22 10:38:01 +02:00
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]