Don't tag instance fields with ThreadStaticAttribute
Warns when System.ThreadStaticAttribute tags instance fields: per documentation it is exclusively for static fields. https://msdn.microsoft.com/en-us/library/system.threadstaticattribute
How to fix
Ensure all fields tagged ThreadStaticAttribute are static.
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>Don't tag instance fields with ThreadStaticAttribute</Name>
// <Id>ND2504:DontTagInstanceFieldsWithThreadStaticAttribute</Id>
warnif count > 0
from f in Application.Fields
where !f.IsStatic &&
f.HasAttribute ("System.ThreadStaticAttribute".AllowNoMatch())
select new {
f,
Debt = 10.ToMinutes().ToDebt(),
Severity = Severity.High
}
//<Expl>
// The *instance {0}* shouldn't be tagged with *ThreadStaticAttribute*.
// This attribute is intended to be used exclusively
// with fields that are declared as *static*.
//</Expl>
//<Desc>
// Warns when **System.ThreadStaticAttribute** tags *instance* fields: per
// documentation it is exclusively for *static* fields.
// https://msdn.microsoft.com/en-us/library/system.threadstaticattribute
//</Desc>
//<HowToFix>
// Ensure all fields tagged *ThreadStaticAttribute* are *static*.
//</HowToFix>
Open in the interactive Rules Explorer · All default rules
.NET Framework Usage / System.Threading: all 6 items
- Don't create threads explicitly ND2500
- Don't use dangerous threading methods ND2501
- Monitor TryEnter/Exit must be both called within the same method ND2502
- ReaderWriterLock AcquireLock/ReleaseLock must be both called within the same method ND2503
- Don't tag instance fields with ThreadStaticAttribute ND2504
- Method non-synchronized that read mutable states ND2505