CS.CTOR.VIRTUAL
构造函数调用其类中定义的虚拟方法
漏洞与风险
调用虚拟方法时,到运行时间前,不会选择执行该方法的实际类型。构造函数调用虚拟方法时,调用该方法的实例的构造函数可能尚未执行。
外部指导
示例 1
复制
namespace NameSpace {
class BadlyConstructedType {
public BadlyConstructedType() {
DoBusiness(); // defect - call to a virtual method
}
public virtual void DoBusiness() {
// doing business...
}
}
public class DerivedType : BadlyConstructedType {
public DerivedType () {}
public override void DoBusiness() {
// this method is may be called when the corresponding object is not constructed...
}
}
}