C# 实现IDisposable的模式

来自MSDN官方文档:http://msdn.microsoft.com/en-us/library/system.configuration.provider.providercollection.aspx

 

 1 using System;
 2 using System.ComponentModel;
 3 
 4 // The following example demonstrates how to create 
 5 // a resource class that implements the IDisposable interface 
 6 // and the IDisposable.Dispose method. 
 7 
 8 public class DisposeExample
 9 {
10     // A base class that implements IDisposable. 
11     // By implementing IDisposable, you are announcing that 
12     // instances of this type allocate scarce resources. 
13     public class MyResource: IDisposable
14     {
15         // Pointer to an external unmanaged resource. 
16         private IntPtr handle;
17         // Other managed resource this class uses. 
18         private Component component = new Component();
19         // Track whether Dispose has been called. 
20         private bool disposed = false;
21 
22         // The class constructor. 
23         public MyResource(IntPtr handle)
24         {
25             this.handle = handle;
26         }
27 
28         // Implement IDisposable. 
29         // Do not make this method virtual. 
30         // A derived class should not be able to override this method. 
31         public void Dispose()
32         {
33             Dispose(true);
34             // This object will be cleaned up by the Dispose method. 
35             // Therefore, you should call GC.SupressFinalize to 
36             // take this object off the finalization queue 
37             // and prevent finalization code for this object 
38             // from executing a second time.
39             GC.SuppressFinalize(this);
40         }
41 
42         // Dispose(bool disposing) executes in two distinct scenarios. 
43         // If disposing equals true, the method has been called directly 
44         // or indirectly by a user's code. Managed and unmanaged resources 
45         // can be disposed. 
46         // If disposing equals false, the method has been called by the 
47         // runtime from inside the finalizer and you should not reference 
48         // other objects. Only unmanaged resources can be disposed. 
49         protected virtual void Dispose(bool disposing)
50         {
51             // Check to see if Dispose has already been called. 
52             if(!this.disposed)
53             {
54                 // If disposing equals true, dispose all managed 
55                 // and unmanaged resources. 
56                 if(disposing)
57                 {
58                     // Dispose managed resources.
59                     component.Dispose();
60                 }
61 
62                 // Call the appropriate methods to clean up 
63                 // unmanaged resources here. 
64                 // If disposing is false, 
65                 // only the following code is executed.
66                 CloseHandle(handle);
67                 handle = IntPtr.Zero;
68 
69                 // Note disposing has been done.
70                 disposed = true;
71 
72             }
73         }
74 
75         // Use interop to call the method necessary 
76         // to clean up the unmanaged resource.
77         [System.Runtime.InteropServices.DllImport("Kernel32")]
78         private extern static Boolean CloseHandle(IntPtr handle);
79 
80         // Use C# destructor syntax for finalization code. 
81         // This destructor will run only if the Dispose method 
82         // does not get called. 
83         // It gives your base class the opportunity to finalize. 
84         // Do not provide destructors in types derived from this class.
85         ~MyResource()
86         {
87             // Do not re-create Dispose clean-up code here. 
88             // Calling Dispose(false) is optimal in terms of 
89             // readability and maintainability.
90             Dispose(false);
91         }
92     }
93     public static void Main()
94     {
95         // Insert code here to create 
96         // and use the MyResource object.
97     }
98 }
View Code


一种处理非托管资源的经典模式:

当手动调用Dispose时,要释放该对象的托管和非托管的资源;

当CG调用Dispose时,要释放该对象的非托管资源,以防遗漏

posted @ 2013-07-31 11:37  MarkSun  阅读(582)  评论(0编辑  收藏  举报