Friday, March 4, 2011

VS2005 Code Analysis: CA1063 (call dispose(true) and supress finalize) - with logging.

I'm trying to adhere to the VS2005 code analysis rules for a new project. I have this method:

public void Dispose()
{
    Console.WriteLine("Dispose() called");
    Dispose( true );
    GC.SuppressFinalize(this);
}

(The console call will become a log4net call at some point, and we're always interested in logging dispose for some of our types).

The presense of the console debug line triggers this error:

Error 1 CA1063 : Microsoft.Design : Modify PerfectDisposableClass.Dispose():Void so that it calls Dispose(true), then calls GC.SuppressFinalize on the current object instance ('this' or 'Me' in VB), and then returns. F:\Visual Studio 2005\Projects\DisposeAndFinalize\DisposeAndFinalize\PerfectDisposableClass.cs 26 DisposeAndFinalize

Without resorting to pragma directives is there a way round this? Perhaps writing my own rules? Also, aer the existing rules in an assembly somewhere I can look at with reflector?

From stackoverflow
  • You could put the log statement inside the Dispose(bool disposing) method to keep within the guidelines:

    public void Dispose(bool disposing)
    {
      if (disposing)
      {
         Console.WriteLine("...");
      }
      ...
    }
    
    ng5000 : Simple answers are always the best! I had log calls in my Dispose(), Dispose( bool ) and finalizer. No need for the log calles in Dispose() or the finalizer, I can just add them to Dispose( bool ) as you suggested, thanks.

0 comments:

Post a Comment