C#不会覆盖重写方法

Hig*_*igh 2 c#

有没有attributepattern告诉编译器不允许覆盖可覆盖的方法?

例如:

Vehicle

public class Vehicle
{
    public virtual void Start() { }
}
Run Code Online (Sandbox Code Playgroud)

Car

public class Car : Vehicle
{
    // ################
    [DontAllowOverrideAgain] //I need something like this attribute
    // ################
    public override void Start()
    {
        // Todo => codes that every car must invoke before start ...
        CarStart();
        // Todo => codes that every car must invoke after start ...
    }

    public virtual void CarStart() { }
}
Run Code Online (Sandbox Code Playgroud)

CoupeCar

public class CoupeCar : Car
{
    // throw and error or show a message to developer
    public override void Start() { }

    public override void CarStart() { }
}
Run Code Online (Sandbox Code Playgroud)

Ice*_*kle 5

当然,只需创建第一个覆盖sealed,这将导致开发人员可以看到的编译时失败

public class Car : Vehicle
{
    public sealed override void Start()
    {
        // Todo => codes that every car must invoke before start ...
        CarStart();
        // Todo => codes that every car must invoke after start ...
    }

    public virtual void CarStart() { }
}
Run Code Online (Sandbox Code Playgroud)