/SerializableExceptionFixer

Checks if class is an Exception and if it is serializable

Primary LanguageC#GNU Affero General Public License v3.0AGPL-3.0

SerializableExceptionFixer

Checks if class is an Exception and if it is serializable. Any error is reported.

In order to be serializable, an exception implementation has to satisfy different conditions mentioned in this blog and this stackoverflow answer:

  • It has to be decorated with Serializable-Attribute
  • There has to be a serialization constructor

Furthermore there are some recommendations for exceptions:

  • Exceptions should have the Suffix Exception
  • There should be a default constructor
  • There should be a constructor that accepts a string
  • There should be a constructor that accpets a string and an Exception
  • There should be a serialization constructor
[Serializable]
public class MyException : Exception
{
    public MyException()
    {
    }

    public MyException(string message)
        : base(message)
    {
    }

    public MyException(string message, Exception inner)
        : base(message, inner)
    {
    }

    protected MyException(SerializationInfo info, StreamingContext context) : base(info, context)
    {
    }
}

If thy custom exception has additional properties, these have to be public with getter and setter. Furthermore they have to be serialized by hand, implementing the ISerializable interface.

Usage

The analyzer can be user in two ways:

  • via Visual Studio Extension
  • via NuGet

Visual Studio Extension

You can download the extension from Visual Studio Gallery.

Advantages

  1. Analyzers and CodeFixes are availbale in all your projects you open.

Disadvantages

  1. Only open files get analyzed
  2. It only works on each machine, where this extension is installed. If your collegue makes misstakes while implementing an exception, this extension won't help until you open the class.
  3. The analyzers mark all results as errors, ut Visual Studio don't treat them as compile time errors.

NuGet

Advantages

  1. Analzers are part of the project and therefore each develloper has to follow the proposed rules.
  2. All files get analyzers during build of the projects.
  3. Errors are compile time errors.

Disadvantages

  1. Analyzers are only available in projects, that references them.
  2. Each project has to reference this NuGet-package on its own.

Available Diagnostics

Id Category Short Description Codefix availbale
SE1010 Serialization [Serilizable] Attribute is missing in future
SE1020 Serialization Serialization constructor is missing in future
SE2010 Convenience Parameterless constructor is missing in future
SE2020 Convenience Constructor that accepts string is missing in future
SE2030 Convenience Constructor that accepts string and Exception is missing in future
SE3010 Naming Exceptions should have suffix Exception in future

If you are interessted in the diagnostics currently availbale, see Diagnostics.

TODO

  • add description and examples for exception with custom properties.

  • add some analyzers

    • analyze that properties and/or fields are used in constructor
    • analyze that GetObjectData() is overwritten when using fields and/or properties
    • analyze that fields and/or properties get serialized in GetObjectData() method.
  • write CodeFix that makes an exception serializable

    • add [Serializable] Attribute
    • implement constructors and assign properties and fields
    • overwrite GetObjectData() and serialize properties and fields

References:

CA Diagnostics: