Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would do something like that using <a href="http://www.postsharp.org/aop-net/overview" rel="nofollow noreferrer">PostSharp</a>.</p> <p>It runs as an extra build step and inserts the corresponding MSIL, but it will do what you want. The PostSharp attribute definitions would look something like this (depending on your exact implementation). These could then be used as you describe above.</p> <pre><code>public sealed class ReadOperationAttribute : OnMethodBoundaryAspect { // not quite sure how you manage your lock, so put this dummy method in. ReaderWriterLock _rwLock = ReaderWriterLock.GetCorrectReaderWriterLock(); [ThreadStatic] static bool _isLocked; public override void OnEntry( MethodExecutionEventArgs e ) { try { _rwLock.AcquireReaderLock(10000); _isLocked = true; } catch { _isLocked = false; throw; } } public override void OnExit( MethodExecutionEventArgs e ) { if (_isLocked) { _rwLock.ReleaseReaderLock(); _isLocked = false; } } } public sealed class WriteOperationAttribute : OnMethodBoundaryAspect { // not quite sure how you manage your lock, so put this dummy method in. ReaderWriterLock _rwLock = ReaderWriterLock.GetCorrectReaderWriterLock(); [ThreadStatic] static bool _isLocked; public override void OnEntry( MethodExecutionEventArgs e ) { try { _rwLock.AcquireWriterLock(10000); _isLocked = true; } catch { _isLocked = false; throw; } } public override void OnExit( MethodExecutionEventArgs e ) { if (_isLocked) { _rwLock.ReleaseReaderLock(); _isLocked = false; } } } </code></pre> <p>EDIT: Updated to address concerns. (Untested ;) ) Also, note as I said in the comment to the question, Microsoft <a href="http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlock.aspx" rel="nofollow noreferrer">recommends</a> using <code>ReaderWriterLockSlim</code> in preference to <code>ReaderWriterLock</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload