Is this a good Dispose() idea or not?

  • Thread starter Thread starter first10
  • Start date Start date
F

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
 
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.
 
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.)



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

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
 
Back
Top