Mark ISerializable types with SerializableAttribute

Rule ND2200 .NET Framework Usage / System

The CLR recognizes types as serializable only when marked with SerializableAttribute, even with custom serialization via ISerializable. Matches ISerializable types not tagged with it.

How to fix

Tag the matched type with SerializableAttribute.

CQLinq source

This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.

// <Name>Mark ISerializable types with SerializableAttribute</Name>
// <Id>ND2200:MarkISerializableTypesWithSerializableAttribute</Id>

warnif count > 0
          
from t in Application.Types where 
  t.IsPublic && 
 !t.IsDelegate &&
 !t.IsExceptionClass && // Don't match exceptions, since the Exception class
                        // implements ISerializable, this would generate
                        // too many false positives.
  t.Implement ("System.Runtime.Serialization.ISerializable".AllowNoMatch()) && 
 !t.HasAttribute ("System.SerializableAttribute".AllowNoMatch())

select new {
   t,
   t.NbLinesOfCode,
   Debt = 5.ToMinutes().ToDebt(),
   Severity = Severity.High
}

//<Expl>
// The *{0}* should be tagged with *SerializableAttribute*
// to be recognized by the runtime as serializable.
//</Expl>

//<Desc>
// The CLR recognizes types as serializable only when marked with
// *SerializableAttribute*, even with custom serialization via
// *ISerializable*. Matches *ISerializable* types not tagged with it.
//</Desc>

//<HowToFix>
// Tag the matched type with *SerializableAttribute*.
//</HowToFix>

.NET Framework Usage / System: all 12 items