Writing high-quality code to improve the C # program's 157 suggestions-Recommendation 49: a protected virtual method should be extracted in the Dispose mode,
Suggestion 49: a protected virtual method should be extracted in Dispose mode.
In the standard Dispose mode, the real IDisposable interface's Dispose method does not actually clean up. It actually calls the following protected virtual method with the bool parameter:
/// <Summary> // use protected virtual for non-sealed class modification // use private for sealing class modification /// </summary> // <param name = "disposing "> </param> protected virtual void Dispose (bool disposing) {
// Omitting code}
This protected virtual method is provided because this type is inherited by other types. If a subclass exists, the subclass may implement its own Dispose mode. Protected virtual methods are used to remind subclass: You must note the cleanup of the parent class when cleaning your own method. That is, the subclass must call the base. Dispose method in its own release method.
Public class DerivedSampleClass: SampleClass {// the unmanaged resource of the subclass private IntPtr derivedNativeResource = Marshal. allocHGlobal (100); // The Managed Resource private AnotherResource derivedManagedResource of the subclass = new AnotherResource (); // defines whether to release its own identity variable private bool derivedDisposed = false; /// <summary> /// override the Dispose method of the parent class /// </summary> /// <param name = "disposing"> </param> protected override void Dispose (bool disposing) {if (de RivedDisposed) {return;} if (disposing) {// clear managed resources if (derivedManagedResource! = Null) {derivedManagedResource. Dispose (); derivedManagedResource = null ;}// clear unmanaged resources if (derivedNativeResource! = IntPtr. zero) {Marshal. freeHGlobal (derivedNativeResource); derivedNativeResource = IntPtr. zero;} // call the cleanup code base of the parent class. dispose (disposing); // Let the type know that it has been released derivedDisposed = true;} public class SampleClass: IDisposable {// demonstrate creating an unmanaged resource private IntPtr nativeResource = Marshal. allocHGlobal (100); // demonstrate creating a managed resource private AnotherResource managedResource = new AnotherResource (); private bool disposed = false; /// <Summary> /// implement the Dispose method in IDisposable /// </summary> public void Dispose () {// it must be true Dispose (true ); // notify the garbage collection mechanism not to call the final generator (destructor) GC. suppressFinalize (this);} // <summary> // It is not necessary to provide a Close method to better comply with other languages (such as // C ++) // </summary> public void Close () {Dispose () ;}/// <summary> // required, prevent programmers from explicitly calling the Dispose method. /// </summary> ~ SampleClass () {// It must be false Dispose (false );} /// <summary> // use protected virtual for non-sealed class modification // use private for sealing class modification /// </summary> // <param name = "disposing "> </param> protected virtual void Dispose (bool disposing) {if (disposed) {return;} if (disposing) {// clear managed resources if (managedResource! = Null) {managedResource. Dispose (); managedResource = null ;}// clear unmanaged resources if (nativeResource! = IntPtr. zero) {Marshal. freeHGlobal (nativeResource); nativeResource = IntPtr. zero;} // Let the type know that it has been released disposed = true;} public void SamplePublicMethod () {if (disposed) {throw new ObjectDisposedException ("SampleClass ", "SampleClass is disposed");} // omitted} class AnotherResource: IDisposable {public void Dispose (){}}
If this protected virtual method is not provided for the class, it is very likely that the developer will ignore the cleaning of the parent class when designing the subclass. Therefore, you must provide a protected virtual method in the Dispose mode.