IDisposable接口
User.cs
public class User { public int id { get; set; } public string name { get; set; } }
BaseService.cs
public abstract class BaseService:IDisposable { /// <summary> /// 要释放的对象 /// </summary> public IList<IDisposable> DisposablesObjects { get; private set; } public User LoginUser { get; set; } protected BaseService() { this.DisposablesObjects=new List<IDisposable>(); } protected void AddDisposableObject(object obj) { IDisposable disposable = obj as IDisposable; if (null != disposable) { this.DisposablesObjects.Add(disposable); } } void IDisposable.Dispose() { foreach (IDisposable obj in this.DisposablesObjects) { if (null != obj) { obj.Dispose(); } } } }