ReaderWriterLock AcquireLock/ReleaseLock must be both called within the same method

Rule ND2503 .NET Framework Usage / System.Threading

Warns when System.Threading.ReaderWriterLock acquire and release (reader or writer) methods are not called within the same method: harder to locate where critical sections begin and end, and complex error-prone scenarios.

How to fix

Refactor so ReaderWriterLock read or write critical sections begin and end in the same method.

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>ReaderWriterLock AcquireLock/ReleaseLock must be both called within the same method</Name>
// <Id>ND2503:ReaderWriterLockAcquireLockReleaseLockMustBeBothCalledWithinTheSameMethod</Id>

warnif count > 0 

let acquireLockMethods = ThirdParty.Methods.WithFullNameWildcardMatch(
    "System.Threading.ReaderWriterLock.Acquire*Lock(*")

let releaseLockMethods = ThirdParty.Methods.WithFullNameWildcardMatch(
    "System.Threading.ReaderWriterLock.Release*Lock(*")

from m in Application.Methods.UsingAny(acquireLockMethods)
  .Except(Application.Methods.UsingAny(releaseLockMethods))
select new { 
   m, 
   acquireLockMethods = m.MethodsCalled.Intersect(acquireLockMethods),
   Debt = 20.ToMinutes().ToDebt(),
   Severity = Severity.High
}

//<Expl>
// The *{0}* calls *{1}* but doesn't call corresponding *ReaderWriterLock.ReleaseLock()* method{1?:s}.
// Refactor the code so that a *critical section* is contained within a single method.
//</Expl>

//<Desc>
// Warns when **System.Threading.ReaderWriterLock** acquire and release
// (reader or writer) methods are not called within the same method: harder
// to locate where **critical sections** begin and end, and complex
// error-prone scenarios.
//</Desc>

//<HowToFix>
// Refactor so *ReaderWriterLock read or write critical sections* begin and
// end in the same method.
//</HowToFix>

.NET Framework Usage / System.Threading: all 6 items