This class wraps any Type to allow it to be used only within the context of a using statement. It allows for implicit casting to T so it is pretty transpartent.
Example usage:
using(string scoped_string = Scope<string>("Hello, World!"))
{
Console.WriteLine(scoped_string);
}
</string>
The class:
public sealed class Scope<t>: IDisposable where T: new()
{
public Scope(T value)
{
_Value = value;
}
public Scope()
{
_Value = new T();
}
public static implicit operator T(Scope<t> scope)
{
return scope._Value;
}
private bool _Disposed = false;
private T _Value;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose(bool disposing)
{
if (_Disposed) return;
_Disposed = true;
}
}
</t></t>