Home All Groups Group Topic Archive Search About

Is this a good Dispose() idea or not?

Author
7 Dec 2006 12:10 PM
first10
use reflection to detect class and instance fields which require
disposal
you can derive from a base class with these methods and it will
automatically dispose

void IDisposable.Dispose() {
    DisposeObject(this);
    GC.SuppressFinalize(this);
}//method
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - -
static void DisposeObject(object instance) {
    FieldInfo[] fis = instance.GetType().GetFields(BindingFlags.Instance |
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
    foreach (FieldInfo fi in fis) {
        DisposeField(fi.GetValue(instance));
    }//foreach
}//method
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - -
static void DisposeField(object o) {
    if (o == null) return;
    MethodBase mb = o.GetType().GetMethod("Dispose");
    if (mb == null) return;
    mb.Invoke(o, new object[] {});
}//method

Author
11 Dec 2006 12:46 AM
Alvin Bruney [MVP]
No, it's not. Implement the dispose pattern correctly and you don't need to
probe assemblies to intelligently determine what needs to be disposed. In
any case, reflection imposes a performance penalty on executing code that is
really magnified during garbage collection.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
www.lulu.com/owc

<firs***@btinternet.com> wrote in message
Show quote
news:1165493444.718996.273490@16g2000cwy.googlegroups.com...
> use reflection to detect class and instance fields which require
> disposal
> you can derive from a base class with these methods and it will
> automatically dispose
>
> void IDisposable.Dispose() {
> DisposeObject(this);
> GC.SuppressFinalize(this);
> }//method
> // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> - - - - - - - - - - - - - - - - - - - - - - - - - -
> static void DisposeObject(object instance) {
> FieldInfo[] fis = instance.GetType().GetFields(BindingFlags.Instance |
> BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
> foreach (FieldInfo fi in fis) {
> DisposeField(fi.GetValue(instance));
> }//foreach
> }//method
> // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> - - - - - - - - - - - - - - - - - - - - - - - - - -
> static void DisposeField(object o) {
> if (o == null) return;
> MethodBase mb = o.GetType().GetMethod("Dispose");
> if (mb == null) return;
> mb.Invoke(o, new object[] {});
> }//method
>
Author
24 Dec 2006 12:48 PM
first10
I profiled some code running with my automatic disposal compared with
manual disposal

My automatic disposal is 0.2 milliseconds slower

It made for a 2 second difference across 100,000 iterations.

Finalization was totally unaffected whether I used reflection or not
(I can't see any reason why using Reflection would affect Garbage
Collection?)

Only objects with lifetimes of less than 20 milliseconds would see more
than a 1% performance hit from my technique.

So it seems to be a good idea

(If only retrieving the current StackFrame was as fast I use the same
technique to dispose method variables.)




Alvin Bruney [MVP] wrote:
Show quote
> No, it's not. Implement the dispose pattern correctly and you don't need to
> probe assemblies to intelligently determine what needs to be disposed. In
> any case, reflection imposes a performance penalty on executing code that is
> really magnified during garbage collection.
>
> --
> Regards,
> Alvin Bruney
> ------------------------------------------------------
> Shameless author plug
> Excel Services for .NET is coming...
> OWC Black book on Amazon and
> www.lulu.com/owc
>
> <firs***@btinternet.com> wrote in message
> news:1165493444.718996.273490@16g2000cwy.googlegroups.com...
> > use reflection to detect class and instance fields which require
> > disposal
> > you can derive from a base class with these methods and it will
> > automatically dispose
> >
> > void IDisposable.Dispose() {
> > DisposeObject(this);
> > GC.SuppressFinalize(this);
> > }//method
> > // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> > - - - - - - - - - - - - - - - - - - - - - - - - - -
> > static void DisposeObject(object instance) {
> > FieldInfo[] fis = instance.GetType().GetFields(BindingFlags.Instance |
> > BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
> > foreach (FieldInfo fi in fis) {
> > DisposeField(fi.GetValue(instance));
> > }//foreach
> > }//method
> > // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> > - - - - - - - - - - - - - - - - - - - - - - - - - -
> > static void DisposeField(object o) {
> > if (o == null) return;
> > MethodBase mb = o.GetType().GetMethod("Dispose");
> > if (mb == null) return;
> > mb.Invoke(o, new object[] {});
> > }//method
> >

AddThis Social Bookmark Button