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