DoNotDeclareReadOnlyMutableReferenceTypes is a rule that checks for visible read-only fields that are mutable reference types (classes). A mutable type is a type whose instance data can be changed once it has been constructed. For example, the following type is considered mutable:
Whereas the following type is not, and is therefore considered immutable:
Although not strictly required, I find it good practice to place the readonly modifier on any member that shouldn’t be changed outside of the constructor.
Unfortunately, due to the complex and costly analysis required to differentiate between a mutable type and an immutable type, DoNotDeclareReadOnlyMutableReferenceTypes, simply fires on any reference type that isn’t in its list of known immutable Framework types.
For example, given the above two types, the following declarations would both be considered violations, even though the Immutable type is immutable:
However, it is possible to change this. To make DoNotDeclareReadOnlyMutableReferenceTypes aware of your immutable types, simply do the following:
Note: This requires FxCop 1.32 or above.
As always, if you have any questions or issues with FxCop or Managed Code Analysis (including the DoNotDeclareReadOnlyMutableReferenceTypes) head over to the FxCop Forum.