I’ve started using Microsoft’s Code Contracts and finding some interesting interactions with Code Analysis (aka fxCop), a feature of Visual Studio that I’m already using heavily.

When writing a method to check contract invariants, Code Contracts wants me to declare it as a private method containing only calls to Contract.Invariant().

However, Code Analysis sees this and tells me I shouldn’t have an uncalled private method.

Normally, the advice from Code Analysis is correct - but in this case, it needes suppressing like so:

[ContractInvariantMethod]
[SuppressMessage("Microsoft.Performance", 
                 "CA1811:AvoidUncalledPrivateCode")]
private void ContractInvariants()
{
    Contract.Invariant(mBody != null);
    Contract.Invariant(mRecommendation != null);
    Contract.Invariant(mConverter != null);
}

Comments

blog comments powered by Disqus