Mark ISerializable types with SerializableAttribute
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>
Open in the interactive Rules Explorer · All default rules
.NET Framework Usage / System: all 12 items
- Mark ISerializable types with SerializableAttribute ND2200
- Mark assemblies with CLSCompliant (deprecated) ND2201
- Mark assemblies with ComVisible (deprecated) ND2202
- Mark attributes with AttributeUsageAttribute ND2203
- Remove calls to GC.Collect() ND2204
- Don't call GC.Collect() without calling GC.WaitForPendingFinalizers() ND2205
- Enum Storage should be Int32 ND2206
- Do not raise too general exception types ND2207
- Do not raise reserved exception types ND2208
- Uri fields or properties should be of type System.Uri ND2209
- Types should not derive from System.ApplicationException ND2210
- Don't Implement ICloneable ND2211