I often used a one-time template in simple classes that referenced a small amount of resources, but I never had to implement this template for a class that inherits from another one-time class, and I'm starting to get a little confused about how to free up whole resources.
I start with a small code example:
public class Tracer : IDisposable
{
bool disposed;
FileStream fileStream;
public Tracer()
{
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if (fileStream != null)
{
fileStream.Dispose();
}
}
disposed = true;
}
}
}
public class ServiceWrapper : Tracer
{
bool disposed;
ServiceHost serviceHost;
public ServiceWrapper ()
{
}
}
My real question is: how to implement a one-time template inside my ServiceWrapper class to make sure that when I delete its instance, it will have resources in both the inherited and the base class?
Thank.
source
share