c# - Automatic Disposal Extension Method reasonable? -


i've been writing custom winform controls pretty heavy amount of drawing , therefore tend have lot of disposable graphics based fields lying around (brushes, pens, bitmaps etc..) , result control's dispose() method has call dispose on each of them.

i got concerned (or future maintainer) miss field needs disposed, either forgetting dispose or not realising implements idisposable. such wrote simple extension method on object finds idisposable fields , disposes of them:

static public void disposeall(this object obj) {    var disposable = obj.gettype()                        .getfields(bindingflags.nonpublic | bindingflags.public | bindingflags.instance)                        .select(fi => fi.getvalue(obj))                        .where(o => o != null && o idisposable)                        .cast<idisposable>();     foreach (var d in disposable) d.dispose(); } 

my question whether reasonable thing do. can't think might screw up, i'm not particularly familiar inner workings of winforms , seems kind of thing (messing reflection , disposing) might cause irritating bugs down line.

usually don't want dispose disposable members. e.g. reference parent form.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -