Design Pattern - IDisposable Pattern(C#)

来源:互联网 发布:新业汽修软件 编辑:程序博客网 时间:2024/04/30 23:32

// --------------------------------------------------------------------------------------------------------------------// <copyright file="DisposeExample.cs" company="Chimomo's Company">//   Respect the work.// </copyright>// <summary>//   Defines the DisposeExample type.// </summary>// --------------------------------------------------------------------------------------------------------------------// The following example demonstrates how to create a resource class that implements the IDisposable interface and the IDisposable.Dispose method.namespace CSharpLearning{    using System;    using System.ComponentModel;    /// <summary>    /// The dispose example.    /// </summary>    public static class DisposeExample    {        /// <summary>        /// The main.        /// </summary>        public static void Main()        {            // Insert code here to create and use the MyResource object.        }        /// <summary>        /// The my resource.        /// A base class that implements IDisposable. By implementing IDisposable, you are announcing that instances of this type allocate scarce resources.        /// </summary>        public class MyResource : IDisposable        {            /// <summary>            /// The handle.            /// Pointer to an external unmanaged resource.            /// </summary>            private IntPtr handle;            /// <summary>            /// The component.            /// Other managed resource this class uses.            /// </summary>            private readonly Component component = new Component();            /// <summary>            /// The disposed.            /// Track whether Dispose has been called.            /// </summary>            private bool disposed = false;            /// <summary>            /// Initializes a new instance of the <see cref="MyResource"/> class.            /// The class constructor.            /// </summary>            /// <param name="handle">            /// The handle.            /// </param>            public MyResource(IntPtr handle)            {                this.handle = handle;            }            /// <summary>            /// The dispose.            /// Implement IDisposable. Do not make this method virtual. A derived class should not be able to override this method.            /// </summary>            public void Dispose()            {                this.Dispose(true);                // This object will be cleaned up by the Dispose method. Therefore, you should call GC.SupressFinalize to take this object off the finalization queue and prevent finalization code for this object from executing a second time.                GC.SuppressFinalize(this);            }            /// <summary>            /// The dispose.            /// Dispose(bool disposing) executes in two distinct scenarios.            /// If disposing equals true, the method has been called directly or indirectly by a user's code. Managed and unmanaged resources can be disposed.            /// If disposing equals false, the method has been called by the runtime from inside the finalizer and you should not reference other objects. Only unmanaged resources can be disposed.            /// </summary>            /// <param name="disposing">            /// The disposing.            /// </param>            protected virtual void Dispose(bool disposing)            {                // Check to see if Dispose has already been called.                if (!this.disposed)                {                    // If disposing equals true, dispose all managed and unmanaged resources.                    // Dispose managed resources.                    if (disposing)                    {                        this.component.Dispose();                    }                    // Call the appropriate methods to clean up unmanaged resources here.                    // If disposing is false, only the following code is executed.                    CloseHandle(this.handle);                    this.handle = IntPtr.Zero;                    // Note disposing has been done.                    this.disposed = true;                }            }            // Use interop to call the method necessary to clean up the unmanaged resource.            [System.Runtime.InteropServices.DllImport("Kernel32")]            private static extern bool CloseHandle(IntPtr handle);            /// <summary>            /// Finalizes an instance of the <see cref="MyResource"/> class.            /// Use C# destructor syntax for finalization code. This destructor will run only if the Dispose method does not get called. It gives your base class the opportunity to finalize.            /// Do not provide destructors in types derived from this class.            /// </summary>            ~MyResource()            {                // Do not re-create Dispose clean-up code here. Calling Dispose(false) is optimal in terms of readability and maintainability.                this.Dispose(false);            }        }    }}

原创粉丝点击