views:

612

answers:

1

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?

+2  A: 

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("...");
  }
  ...
}
Rob Walker
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.
ng5000