ImplementIDisposableCorrectly

The rule “ImplementIDisposableCorrectly” should not be displayed in sealed classes as the Dispose(bool) method cannot be declared as protected or virtual.

Also, the help page for this rule has no text for this rule and when clicking the feedback button, it generates an e-mail with the subject ofInterfaceNamesHaveIPrefix

Cheers,

Steve

[925 byte] By [SteveDunn] at [2008-2-22]
# 1
Can you post a code example that shows the class that FxCop is picking up?

The only way I can FxCop to pick up a sealed class is by doing the following:



public sealed class Disposable : IDisposable
{
void IDisposable.Dispose()
{
}
}

However, this is a violation of the Dispose pattern, where Dispose should be public.

DavidM.Kean at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 2
There's a bug in our generated help system currently that results in the 'InterfaceNamesHaveIPrefix' email subject for any topic with no content. Sorry about that. We'll get this resolved as soon as we can.

Michael

MichaelFanning-MS at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 3
Hi,
Here's the code:


public sealed class Class1
: IDisposable
{
~Class1( ) { Dispose ( false ) ; }

public void Dispose() { Dispose( true ) ; }

bool _disposed ;

public void Dispose( bool disposing )
{
if ( _disposed )
return ;

if ( disposing )
{ }

_disposed = true ;
}
}


Class1 is sealed, so FxCop's suggestion cannot be implemented:
"Ensure that Class1.Dispose(Boolean):Void is declared as protected, virtual, and unsealed."

SteveDunn at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 4
Thanks for posting this pattern. I think the core problem here is that you shouldn't be declaring a public Dispose(bool) method. Why give an object client the opportunity to call this with an incorrect argument? Since you're the originator of the dispose implementation and since you're defining a sealed type, you should simply implement Dispose() and call it good.

We should either modify the rule to flag this as a problem or fix the issue as originally described (stop firing this specific message on a sealed type). I'll talk to our in-house expert on this rule and get some guidance on which approach is best. In any case, we'll certainly take some kind of fix. Thanks again for taking the time to submit a repro,

Michael

MichaelFanning-MS at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...

Visual Studio Team System

Site Classified