Skip to content

Commit

Permalink
Fix S4027 C#: BinaryFormatter. Serialization constructors are obsolet…
Browse files Browse the repository at this point in the history
…e and should not be required (#3541)
  • Loading branch information
cristian-ambrosini-sonarsource authored Jan 18, 2024
1 parent a695895 commit 5a8cb45
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 55 deletions.
15 changes: 15 additions & 0 deletions rules/S4027/csharp/rspecator.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Implement the missing constructors for this exception.

=== Highlighting

Exception class declaration

endif::env-github,rspecator-view[]
83 changes: 28 additions & 55 deletions rules/S4027/csharp/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,56 @@

Exceptions types should provide the following constructors:

* ``++public MyException()++``
* ``++public MyException(string)++``
* ``++public MyException(string, Exception)++``
* ``++protected++`` or ``++private MyException(SerializationInfo, StreamingContext)++``
* `public MyException()`
* `public MyException(string)`
* `public MyException(string, Exception)`
That fourth constructor should be ``++protected++`` in unsealed classes, and ``++private++`` in sealed classes.
The absence of these constructors can complicate exception handling and limit the information that can be provided when an exception is thrown.

== How to fix it

Not having this full set of constructors can make it difficult to handle exceptions.
=== Code examples

==== Noncompliant code example

=== Noncompliant code example

[source,csharp]
[source,csharp,diff-id=1,diff-type=noncompliant]
----
using System;
namespace MyLibrary
public class MyException : Exception // Noncompliant: several constructors are missing
{
public class MyException // Noncompliant: several constructors are missing
public MyException()
{
public MyException()
{
}
}
}
----

==== Compliant solution

=== Compliant solution

[source,csharp]
[source,csharp,diff-id=1,diff-type=compliant]
----
using System;
using System.Runtime.Serialization;
namespace MyLibrary
public class MyException : Exception
{
public class MyException : Exception
public MyException()
{
public MyException()
{
}
public MyException(string message)
:base(message)
{
}
}
public MyException(string message, Exception innerException)
: base(message, innerException)
{
}
public MyException(string message)
:base(message)
{
}
protected MyException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
public MyException(string message, Exception innerException)
: base(message, innerException)
{
}
}
----

== Resources

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Implement the missing constructors for this exception.


=== Highlighting

Exception class declaration
=== Documentation

* Microsoft Learn: https://learn.microsoft.com/en-us/dotnet/standard/exceptions/how-to-create-user-defined-exceptions[How to create user-defined exceptions]
* Microsoft Learn: https://learn.microsoft.com/en-us/dotnet/api/system.exception[Exception Class]
* Microsoft Learn: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/exceptions/creating-and-throwing-exceptions#define-exception-classes[Define exception classes]

endif::env-github,rspecator-view[]
include::./rspecator.adoc[]

0 comments on commit 5a8cb45

Please sign in to comment.