FXCOP's ConstructorsShouseNotcallBaseClassVirtualMethods specification

xiaoxiao2021-03-06  169

There is a Rule ConstructorshouseShouseShouseShouseSsthods in FXCOP that if you call the Virtual method in the constructor? Look at the following code: public class baseclass {public int intecount = 0; public baseclass () {increate ();}

Public virtual void increate () {Intcount ;

}

Public class subclass: baseclass {

PUBLIC INT SUBCOUNT = 0; public subclass () {increate ();

} Public override void increate () {subcount ;}

} private void button1_click (object sender, system.eventargs e) {subclass xx = new subclass (); messagebox.show (xx.intcount.tostring () "---" xx.subcount.toString ());} The result of the display is xx.intcount is 0; xx.subcount is 2; the reason is that subclass's increate () is called before Subclass () is called. After the above code is compiled with FxCop analysis, error will be reported as follows: CriticalWarning, Certainty 95, for "ConstructorsShouldNotCallBaseClassVirtualMethods" {Target: ".ctor ()" (IntrospectionTargetConstructor) Location: "file: c: / documents and settings / administrator.hrms -xam / my documents / visual studio projects / windowsapplication1 / form1.cs (30) "(String) Resolution:" 'BaseClass..ctor ()' contains a call chain that results in a call to a virtual method defined by the class Review The Following Call Stack for Unintended Conses: BaseClass..ctor () baseclass.incade () ".... CTOR () is a constructor. The FXCOP code police are very useful for DotNet developers, at least it can help you improve your code quality.

转载请注明原文地址:https://www.9cbs.com/read-128229.html

New Post(0)