Auto-implement IDispose?

I was just doing some code analysis cleanup on some old code, and ran across
several classes that had the suggestion to implement IDisposable because
they created desposable resources internally.

After going through the exercise (I used ReSharper's Generate unimplemented
members), and realizing there was a definite pattern to the implementations,
I started to wonder if ReSharper couldn't just do most of the work in the
future?  It should be easy to identify the members that are disposable, and
to implement the standard disposable pattern (as guided by the CodeAnalysis
messages).... Either mark the class as sealed and impiment Dispose, or
implement Dispose() as a call to Dispose(true) and then GC.SupressFinalize,
and implement Dispose(bool disposing) to dispose of all the disposable
types, and close any handles.

It might be a little "too narrow", but it sure would be helpful with
ensuring there aren't resource leaks, and in responding to Code Analysis
warnings.


1 comment

Hello Paul,

There is something like that in PowerToys (check ReSharper nightly builds),
which will do some work for you. Or you can take the right pill, update this
PowerToy to be a lot better and send it back to us. We all would appreciate
it! :) 

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


PB> I was just doing some code analysis cleanup on some old code, and
PB> ran across several classes that had the suggestion to implement
PB> IDisposable because they created desposable resources internally.
PB>
PB> After going through the exercise (I used ReSharper's Generate
PB> unimplemented members), and realizing there was a definite pattern
PB> to the implementations, I started to wonder if ReSharper couldn't
PB> just do most of the work in the future?  It should be easy to
PB> identify the members that are disposable, and to implement the
PB> standard disposable pattern (as guided by the CodeAnalysis
PB> messages).... Either mark the class as sealed and impiment Dispose,
PB> or implement Dispose() as a call to Dispose(true) and then
PB> GC.SupressFinalize, and implement Dispose(bool disposing) to dispose
PB> of all the disposable types, and close any handles.
PB>
PB> It might be a little "too narrow", but it sure would be helpful with
PB> ensuring there aren't resource leaks, and in responding to Code
PB> Analysis warnings.
PB>


0

Please sign in to leave a comment.