rspec/rules/S2552/csharp/rule.adoc

72 lines
1.8 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Classes that declare an implementation of ``++Serializable++`` should provide a serializable constructor. Without such a constructor, you'll be unable to deserialize the class.
Serialization constructors should be ``++private++`` for ``++sealed++`` types and ``++protected++`` otherwise.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
[Serializable]
public class Person : ISerializable { // Noncompliant; missing serializable constructor
public void GetObjectData (SerializationInfo info, StreamingContext context) {
// ...
}
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
[Serializable]
public class Person : ISerializable { // Noncompliant; missing serializable constructor
protected Person (SerializationInfo info, StreamingContext context) {
// ...
}
public void GetObjectData (SerializationInfo info, StreamingContext context) {
// ...
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Add a serialization constructor to this class.
'''
== Comments And Links
(visible only on this page)
=== on 4 Feb 2015, 14:09:09 Ann Campbell wrote:
Gendarme MissingSerializationConstructorRule
=== on 13 Apr 2015, 10:48:15 Freddy Mallet wrote:
@Tamas, does this rule makes sense to you ? Thanks
=== on 22 May 2015, 09:21:46 Tamas Vajk wrote:
\[~freddy.mallet] Yes, it makes sense. BUT it is already implemented by the Roslyn team:
https://msdn.microsoft.com/en-us/library/ms182343.aspx
https://github.com/dotnet/roslyn/blob/master/src/Diagnostics/FxCop/Core/Usage/SerializationRulesDiagnosticAnalyzer.cs
=== on 28 May 2015, 12:44:12 Tamas Vajk wrote:
This rule duplicates CA2229 (\https://msdn.microsoft.com/en-us/library/ms182343.aspx), which is already implemented by the Roslyn team.
endif::env-github,rspecator-view[]