CA2214:不要在构造函数中调用可重写的方法
属性 | 值 |
---|---|
规则 ID | CA2214 |
标题 | 不要在构造函数中调用可重写的方法 |
类别 | 使用情况 |
修复是中断修复还是非中断修复 | 非中断 |
在 .NET 8 中默认启用 | 否 |
原因
非密封类型的构造函数调用其类中定义的虚拟方法。
规则说明
当调用虚拟方法时,直到运行时才会选择执行该方法的实际类型。 构造函数调用虚拟方法时,可能尚未执行调用该方法的实例的构造函数。 如果已重写的虚拟方法依赖于构造函数中的初始化和其他配置,则可能导致错误或意外行为。
如何解决冲突
若要解决此规则的冲突,请不要从某个类型的构造函数中调用该类型的虚拟方法。
何时禁止显示警告
不禁止显示此规则发出的警告。 应重新设计构造函数以消除对虚拟方法的调用。
示例
下面的示例演示了与此规则发生冲突的后果。 测试应用程序会创建一个 DerivedType
实例,从而执行其基类 (BadlyConstructedType
) 构造函数。 BadlyConstructedType
的构造函数错误调用了虚拟方法 DoSomething
。 如输出所示,先执行 DerivedType
的构造函数,再执行 DerivedType.DoSomething()
。
public class BadlyConstructedType
{
protected string initialized = "No";
public BadlyConstructedType()
{
Console.WriteLine("Calling base ctor.");
// Violates rule: DoNotCallOverridableMethodsInConstructors.
DoSomething();
}
// This will be overridden in the derived type.
public virtual void DoSomething()
{
Console.WriteLine("Base DoSomething");
}
}
public class DerivedType : BadlyConstructedType
{
public DerivedType()
{
Console.WriteLine("Calling derived ctor.");
initialized = "Yes";
}
public override void DoSomething()
{
Console.WriteLine("Derived DoSomething is called - initialized ? {0}", initialized);
}
}
public class TestBadlyConstructedType
{
public static void Main2214()
{
DerivedType derivedInstance = new DerivedType();
}
}
Imports System
Namespace ca2214
Public Class BadlyConstructedType
Protected initialized As String = "No"
Public Sub New()
Console.WriteLine("Calling base ctor.")
' Violates rule: DoNotCallOverridableMethodsInConstructors.
DoSomething()
End Sub 'New
' This will be overridden in the derived type.
Public Overridable Sub DoSomething()
Console.WriteLine("Base DoSomething")
End Sub 'DoSomething
End Class 'BadlyConstructedType
Public Class DerivedType
Inherits BadlyConstructedType
Public Sub New()
Console.WriteLine("Calling derived ctor.")
initialized = "Yes"
End Sub 'New
Public Overrides Sub DoSomething()
Console.WriteLine("Derived DoSomething is called - initialized ? {0}", initialized)
End Sub 'DoSomething
End Class 'DerivedType
Public Class TestBadlyConstructedType
Public Shared Sub Main2214()
Dim derivedInstance As New DerivedType()
End Sub 'Main
End Class
End Namespace
该示例产生下面的输出:
Calling base ctor.
Derived DoSomething is called - initialized ? No
Calling derived ctor.