c#、VS重写时让一段代码出现在派生class方法中

c#, VS Make a piece of code to appear in derived class method while overriding

有没有办法让一段代码在覆盖时出现在方法中? 就像这样:

    public class BaseClass
    {
      public abstract void SomeMethod()
      {
         //here  want to place a piece of code which I want to use in derived class while overriding (for ex. try-catch block)
         try
         {
         }
         catch
         {
         }

      }
    }

public class DerivedClass : BaseClass
{
   public override void SomeMethod()
   {
      //here I would like for try-catch block to appear like informing me that it is the snippet which makes using of that method is better in this case
   }
}

我不想 运行 来自基础 class 的代码。我只是想让覆盖的方法显示为预先填充了一些来自基础 class 的代码片段,比如告诉我在这种情况下使用该方法更好的代码片段

例如,当我通过 IntelliSense 覆盖 Visual studio 中的方法时,"throw new NotImplementedException();" 文本出现在方法中。我的目标是展示一些其他片段。

你不能做你要求的,但是有两种方法可以做类似的事情。

首先是在您的 class 中声明一个包含代码的方法 - 您的方法调用该方法然后继续。

第二种是创建一个带有抽象方法的抽象class(或者只是一个带有空方法的普通class)。在您的抽象 class 中有一个方法,其中包含您想要的代码并在其中调用抽象方法。

您的新 class 需要实现该抽象方法,但其他代码将在调用抽象方法之前和之后被调用。