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.
The analyzer can be user in two ways:
- via Visual Studio Extension
- via NuGet
You can download the extension from Visual Studio Gallery.
Advantages
- Analyzers and CodeFixes are availbale in all your projects you open.
Disadvantages
- Only open files get analyzed
- 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.
- The analyzers mark all results as errors, ut Visual Studio don't treat them as compile time errors.
Advantages
- Analzers are part of the project and therefore each develloper has to follow the proposed rules.
- All files get analyzers during build of the projects.
- Errors are compile time errors.
Disadvantages
- Analyzers are only available in projects, that references them.
- Each project has to reference this NuGet-package on its own.
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.
-
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:
- Choosing the Right Type of Exception to Throw by Krzysztof Cwalina
- The CORRECT Way to Code a Custom Exception Class by Doug Seelinger
- What is the correct way to make a custom .NET Exception serializable? by Daniel Fortunov and Duncan Jones
- Serialization (C# )
- Object Serialization in the .NET Framework by Piet Obermeyer and Jonathan Hawkins
CA Diagnostics: