Search code examples
.netdependency-propertiescode-analysisfxcop

What is the best way to implement a DependencyProperty and also avoid 'CA2104: Do not declare read only mutable reference types'?


What is the best way (or, is there a way) to implement a dependency property while avoiding the code analysis warning for CA2104 (Do not declare readonly mutable reference types)?

The MSDN documentation suggests this way of declaring your dependency property:

  public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
    "State", typeof(Boolean), typeof(MyStateControl),new PropertyMetadata(false));

But that results in CA2104. It is easy enough to suppress, but I just wondered if there was a better way.


Solution

  • That's a false positive; DependencyProperty is immutable.

    You could use a property instead of a field, but you would then need to set it in a static constructor, triggering another warning.