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
Don't assume shared understanding  25 Jan 2026
Old blog posts, restored  26 Oct 2025
Better Table Tests in Go  21 Oct 2025
Error assertions  26 Apr 2025
Browsers and WSL  31 Mar 2024
Factory methods and functions  05 Mar 2023
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
Archives
February 2013
2013