實現IDisposable的程式碼片段
1 ~DemoType() 2 { 3 this.Dispose(); 4 } 5 6 #region IDisposable Members 7 8 /// <summary> 9 /// Internal variable which checks if Dispose has already been called 10 /// </summary> 11 protected Boolean disposed; 12 13 /// <summary> 14 /// Releases unmanaged and - optionally - managed resources 15 /// </summary> 16 /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> 17 protected void Dispose(Boolean disposing) 18 { 19 if (disposed) 20 { 21 return; 22 } 23 24 if (disposing) 25 { 26 //TODO: Managed cleanup code here, while managed refs still valid 27 } 28 //TODO: Unmanaged cleanup code here 29 30 disposed = true; 31 } 32 33 /// <summary> 34 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. 35 /// </summary> 36 public void Dispose() 37 { 38 // Call the private Dispose(bool) helper and indicate 39 // that we are explicitly disposing 40 this.Dispose(true); 41 42 // Tell the garbage collector that the object doesn't require any 43 // cleanup when collected since Dispose was called explicitly. 44 GC.SuppressFinalize(this); 45 } 46 47 #endregion
CodeSnippet
1 <?xml version="1.0" encoding="utf-8"?> 2 <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> 3 <CodeSnippet Format="1.0.0"> 4 <Header> 5 <Title>Implement IDisposable</Title> 6 <Author>Surviveplus.net</Author> 7 <Description>Define logic of IDisposable implementation.</Description> 8 <Shortcut>dispoase</Shortcut> 9 <SnippetTypes> 10 <SnippetType>Expansion</SnippetType> 11 </SnippetTypes> 12 </Header> 13 <Snippet> 14 <Declarations> 15 <Literal Editable="false"> 16 <ID>classname</ID> 17 <ToolTip>Class Name</ToolTip> 18 <Function>ClassName()</Function> 19 <Default>ClassNamePlaceholder</Default> 20 </Literal> 21 </Declarations> 22 <Code Language="csharp"> 23 <![CDATA[ 24 #region IDisposable Members 25 26 /// <summary> 27 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. 28 /// </summary> 29 public void Dispose() { 30 this.Dispose( true ); 31 GC.SuppressFinalize( this ); 32 } // end sub 33 34 /// <summary> 35 /// Destruct instance of the class. 36 /// </summary> 37 ~$classname$() { 38 this.Dispose( false ); 39 } 40 41 /// <summary> 42 /// Backing field to track whether Dispose has been called. 43 /// </summary> 44 private bool disposedValue = false; 45 46 /// <summary> 47 /// Dispose managed and unmanaged resources of this instance. 48 /// </summary> 49 /// <param name="disposing">If disposing equals true, managed and unmanaged resources can be disposed. If disposing equals false, only unmanaged resources can be disposed. </param> 50 protected virtual void Dispose( bool disposing ) { 51 52 if ( this.disposedValue == false ) { 53 if ( disposing ) { 54 // TODO: Dispose managed resources. 55 } // end if 56 57 // TODO: Dispose unmanaged resources. 58 } // end if 59 60 this.disposedValue = true; 61 } // end sub 62 63 #endregion]]> 64 </Code> 65 </Snippet> 66 </CodeSnippet> 67 </CodeSnippets>
把這段XML文件儲存為dispoase.snippet,儲存到你的VS下面的某個目錄下(我的是(C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\Snippets\2052\Visual C#))
重啟VS,可見效果如圖所示。(這是從Surviveplus Code Snippets發現的一個snippet,非常好用)