Getting from the exception class: CA2237: Mark ISerializable types with the SerializableAttribute parameter

I inferred several classes from various exceptions. Now VS issues a warning as in the title of this question.

  1. Can someone explain what suppressing this rule means?

  2. Could you explain the rule here : "Do not suppress the warning from this rule for exception classes, because they must be serializable to work correctly in application domains."

PS Well, I got the answer myself. You really should mark exceptions as serializable. They work great without this attribute in the same AppDomain. However, if you try to intercept it from another domain, you will need to serialize it to cross the boundaries of the application. And this is the main reason I found for this.

+11
exception compiler-warnings code-analysis
source share
2 answers

This is not exactly a Visual Studio warning, it is a warning created by the FxCop tool. What you can run from the VS Analyze menu. FxCop is a static analyzer that looks for common errors in a .NET program that the compiler will not flag. Most of his warnings are rather obscure and rarely are really serious problems, you need to consider this as "did you think about it?". kind of tool.

The small fact that he is trying to remind you is that the Exception class implements ISerializable and has the [Serializable] attribute. Which is a rather complex requirement, it makes the base Exception object serializable in application domains. Necessary because an exception does not occur from MarshalByRefObject. And you must enable the code that you run in another application domain in order to throw exceptions that you can catch.

So, FxCop notes that you did not do the same for your own derived Exception class. This is really a problem only if you ever intend to have code that causes an exception to be thrown in another application domain. FxCop is not smart enough to know if you do this, it can only remind you that it will go wrong when you do it. This is quite unusual, so feel free to ignore the warning when you still don't know whether you will or not, or if it all sounds like Chinese to you.

+15
source share

If you are not going to use multiple AppDomain applications in your application, I think you can ignore it or suppress it.

+1
source share

All Articles