Friday, May 30, 2014

CA2214 : Do not call overridable methods in constructors Workaround

When you seriously take .net code analysis you may either try to suppress them or try to fix it. There is one such code analysis review if have override or virtual method call in constructor.

 
http://stackoverflow.com/questions/17018569/accessing-an-implemented-abstract-property-in-the-constructor-causes-ca2214-do

http://blogs.msdn.com/b/ericlippert/archive/2008/02/18/why-do-initializers-run-in-the-opposite-order-as-constructors-part-two.aspx

http://blog.duncanworthy.me/swdev/csdotnet/constructor-gotchas-part1/

http://stackoverflow.com/questions/119506/virtual-member-call-in-a-constructor


When an object written in C# is constructed, what happens is that the initializers run in order from the most derived class to the base class, and then constructors run in order from the base class to the most derived class
Problem - Constructor has call to override method.



 
 Workaround :- Add Sealed


 

No comments :