Is this a good Dispose() idea or not?
- From: first10@xxxxxxxxxxxxxx
- Date: 7 Dec 2006 04:10:44 -0800
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
.
- Follow-Ups:
- Re: Is this a good Dispose() idea or not?
- From: Alvin Bruney [MVP]
- Re: Is this a good Dispose() idea or not?
- Prev by Date: accessing eventlogs on remote machine fails using system.management apis
- Next by Date: Re: Dataset ForeignKey Constraint
- Previous by thread: accessing eventlogs on remote machine fails using system.management apis
- Next by thread: Re: Is this a good Dispose() idea or not?
- Index(es):
Relevant Pages
|