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
Next Post
So you should never catch Exception ...  21 Feb 2013
Prior Post
Markdown.XAML  22 Jan 2013
Related Posts
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
When are you done?  18 Apr 2022
Fixing GitHub Authentication  28 Nov 2021
Archives
February 2013
2013