/// <summary> /// 非密封类修饰用protected virtual /// 密封类修饰用private /// </summary> /// <param name="disposing"></param> protected virtual void Dispose(bool disposing) { if (derivedDisposed) { return; } if (disposing) { // 清理托管资源 if (derivedManagedResource != null) { derivedManagedResource.Dispose(); derivedManagedResource = null; } } // 清理非托管资源 if (derivedNativeResource != IntPtr.Zero) { Marshal.FreeHGlobal(derivedNativeResource); derivedNativeResource = IntPtr.Zero; } //调用父类的清理代码 base.Dispose(disposing); //让类型知道自己已经被释放 derivedDisposed = true; }
/// <summary> /// 非密封类修饰用protected virtual /// 密封类修饰用private /// </summary> /// <param name="disposing"></param> protected virtual void Dispose(bool disposing) { if (disposed) { return; } if (disposing) { // 清理托管资源 if (managedResource != null) { managedResource.Dispose(); managedResource = null; } } // 清理非托管资源 if (nativeResource != IntPtr.Zero) { Marshal.FreeHGlobal(nativeResource); nativeResource = IntPtr.Zero; } //让类型知道自己已经被释放 disposed = true; }