Note that there are some explanatory texts on larger screens.

plurals
  1. POCode weave helper for the standard Dispose pattern?
    text
    copied!<p>I have been reading Effective C# and a few other such books/blogs recently and when talking about the <a href="http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx" rel="nofollow">standard Dispose pattern</a> (which I'm already using) they all recommend using the class' <em>dispose</em> variable (as defined in that MSDN sample code) at the beginning of every method. Essentially to insure that once Dispose has been called, any attempt to use the object would result in ObjectDisposedException. This makes sense, but is an enormous amount of manual labor in a large enough code base and relies on humans remembering to do it. So I am looking for a better way.</p> <p>I recently came across and started using the <a href="http://code.google.com/p/notifypropertyweaver/" rel="nofollow">notifypropertyweaver</a> that automatically fills out all the boilerplate code of calling the PropertyChanged handlers (works as an msbuild task, thus requiring no additional shipping dependency). I wonder if anyone knows of a similar solution for the standard dispose pattern. What it would essentially do is accept a variable name as config (the <code>bool disposed</code> in MSDN's sample case), and add the following code to every method that isn't the Finalizer or named Dispose in every class that implements IDisposable:</p> <pre><code>if(disposed) throw new ObjectDisposedException(); </code></pre> <p>Does such a thing exist? Alternatively what do people do to achieve this in their code, manually add the if-statement?</p> <p><strong>Clarification of Purpose</strong><br> The greater need for this is not some "best practice" drive, but that we do have users managing the life-cycles of our objects improperly. Right now they just get a NullReference or some other such thing from underlying resource, which could mean we have a bug in our library, I want to communicate to them that they are the ones creating the issue and how they are crating it (considering I'm in position to know). So suggestions that the users of our Types are the ones who should be taking care of this, isn't really productive here.</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