c# - Code Analysis CA1063 fires when deriving from IDisposable and providing implementation in base class -
c# - Code Analysis CA1063 fires when deriving from IDisposable and providing implementation in base class -
i have code trigger code analysis warning ca1063:
ca1063 : microsoft.design : remove idisposable list of interfaces implemented 'functionality' , override base of operations class dispose implementation instead.
however, i'm not sure need prepare warning.
briefly, have interface ifunctionality derives idisposable. class functionality implements ifunctionality derives class reusable able reuse som code. class reusable derives idisposable.
public class reusable : idisposable { ~reusable() { dispose(false); } public void dispose() { dispose(true); gc.suppressfinalize(this); } protected virtual void dispose(boolean disposing) { // ... } public void dosomething() { // ... } } public interface ifunctionality : idisposable { void dosomething(); void dosomethingelse(); } public class functionality : reusable, ifunctionality { public void dosomethingelse() { // ... } #if work_around_ca1063 // removes ca1063 protected override void dispose(boolean disposing) { base.dispose(disposing); } #endif } i can rid of warning overriding dispose on functionality , calling base of operations class dispose though doing should not alter semantics of code .
so there idisposable in context have overlooked or ca1063 misfires particular construct?
i know can suppress ca1063 rule quite broad , don't want miss other problems in implementing idisposable reported rule.
this false positive due minor bug in rule itself. when trying figure out if class re-implements idisposable (after figuring out there's base of operations class implementation overridden), looks @ whether class' interfaces include idisposable. unfortunately, interface list shows in assembly metadata includes "exploded" list of interfaces, including interfaces inherited via interfaces class explicitly implements in original c# code. means fxcop seeing declaration looks next functionality class:
public class functionality : reusable, ifunctionality, idisposable { ... } given metadata representation, implementidisposablecorrectly rule should bit more intelligent how attempts determine whether class re-implementing idisposable (for example, looking explicit dispose() implementation if base of operations class has overridable dispose(bool)). however, given rule doesn't this, best approach suppress false positives.
btw, recommend considering using suppressmessageattribute suppressing false positives instead of current conditional compilation approach. e.g.:
[suppressmessage("microsoft.design", "ca1063:implementidisposablecorrectly", justification = "false positive. idisposable inherited via ifunctionality. see http://stackoverflow.com/questions/8925925/code-analysis-ca1063-fires-when-deriving-from-idisposable-and-providing-implemen details.")] public class functionality : reusable, ifunctionality { ... } also, might want consider getting rid of finalizer...
c# code-analysis idisposable
I found this blog very helpful and informative. I found answer regarding code analysis tools C#. Thanks for sharing.
ReplyDelete