CA2214 and OnPropertyChanged

Topics: Prism v4 - WPF 4
Jul 25, 2011 at 9:15 PM

I am using Code Analysis in VS2010 Ultimate and I see this warning in my PRISM project.

I am setting data bound properties in my ViewModel overloaded constructor.

Each of these properties have a call made to the OnPropertyChanged in their setters.

Can I use Safe Constructor Patterns for DependencyObjects ( to overcome this problem?

Is this a warning I should take seriously or should I suppress it?

Detailed warning:

Warning 1 CA2214 : Microsoft.Usage : 'ViewModel(View, bool, bool)' contains a call chain that results in a call to a virtual method defined by the class. Review the following call stack for unintended consequences:

ViewModel..ctor(View, Boolean, Boolean)



Jul 26, 2011 at 1:47 PM


When an object is constructed, its constructors are run in order from the base class to the most derived class (in your case, first the constructor in NotificationObject or the class you're using that exposes the RaisePropertyChanged/OnPropertyChanged method, and then your view model class). However, even though the constructors are called in that order, the virtual method calls will point to the override defined in the most derived class (which might be another class that inherits from your view model class); therefore, calling these methods from the constructor might cause some unexpected behavior.

If you mark your ViewModel class as sealed, you will avoid this from happening, since it will be imposible for a class to inherit from it and define a different behavior for the RaisePropertyChanged/OnPropertyChanged method. You will note that, if you mark your class as sealed, the code analysis warning will not appear anymore.

I hope you find this helpful.

Guido Leandro Maliandi

Jul 26, 2011 at 2:04 PM

Thanks Guido for the explanation and usage of "sealed".

In this case sealing the class removes the warning and also ensures that nobody else inherits the ViewModel.

Dec 14, 2011 at 12:25 PM

Thanks also. This is the first explanation that simply explains the message and hwo to resolve it in 99% of cases!!

Thanks Nicholas