Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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