title: The CA2104 warning: Is there any way to mark a class as `Immutable` to suppress it?
body: <p>Consider the following code, which provokes <a href="http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&amp;l=EN-US&amp;k=k%28CA2104%29;k%28TargetFrameworkMoniker-.NETFramework" rel="nofollow noreferrer"><code>CA2104: Do not declare read only mutable reference types.</code></a></p>
  
  <pre><code>public class Test
  {
      // This provokes CA2104: "Do not declare read only mutable reference types".
      protected readonly ImmutableClass ImmutableMember;
  }
  
  public class ImmutableClass
  {
  }
  </code></pre>
  
  <p>Does anyone know of a way to mark a class as immutable in a way that would suppress warning CA2104?</p>
  
  <p>I tried decorating <code>MutableClass</code> with <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.immutableobjectattribute.aspx" rel="nofollow noreferrer"><code>[ImmutableObject(true)]</code></a> with no hope of success (since that attribute is pretty clearly for the Form Editor to use), and sure enough it doesn't work.</p>
  
  <p>I assume that Code Analysis is using a list of known immutable types when determining whether to emit CA2104, so we can't use the same approach.</p>
  
  <p>I guess that even if you <em>could</em> mark a class as immutable, there'd be no way for the compiler to actually check if it was true, but at least it could be a useful indicator.</p>
  
  <p>Anyway, are there any attributes I'm overlooking? If not, suppression will have to do.</p>
  
  <p>It seems that there is no alternative way to do this <em>at the moment</em>.</p>
  
  <p>I did find <a href="http://joeduffyblog.com/2007/11/11/immutable-types-for-c/" rel="nofollow noreferrer">an interesting blog from Joe Duffy (author of "Concurrent Programming On Windows") about this kind of thing</a>.</p>
  
  <p>He starts off with <em>"Imagine we had an ImmutableAttribute."</em>... :)</p>
  
  <p>It's quite interesting - he went to the trouble of writing some new FxCop rules to do some analysis of types attributed as immutable.</p>