实现IDisposable接口的类才能在创建此类的码中使用using来自动释放资源,如:
public class UserBLL : IDisposable
{
public UserBLL(){ }
#region 成员方法
// 成员方法
#endregion
#region 释放类资源
private bool m_disposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!m_disposed)
{
if (disposing)
{
//TODO:此处释放受控资源
}
//TODO:此处释放非受控资源。设置被处理过标记
m_disposed = true;
}
}
~UserBLL()
{
Dispose(false);
}
#endregion
}
调用:
using (UserBLL bll = new UserBLL())
{
//具体的实现代码
}