Is this a good Dispose() idea or not?

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
 
A

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

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



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top