VS2005 Code Analysis: CA1063 (call dispose(true) and supress finalize) - with logging
-
19-08-2019 - |
Question
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?
Solution
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("...");
}
...
}