共用方式為


CA1061:不要隱藏基底類別方法

型別名稱

DoNotHideBaseClassMethods

CheckId

CA1061

分類

Microsoft.Design

中斷變更

中斷

原因

衍生型別 (Derived Type) 會宣告具相同名稱的方法,且其參數個數會與它其中一個基底 (Base) 方法的參數個數相同。其中的一或多個參數會是基底方法中對應參數的基底型別 (Base Type),而剩餘的其他參數則會具有與基底方法中對應參數完全相同的型別。

規則描述

只有在衍生方法的參數簽章因型別衍生時比基底方法參數簽章中的型別還要弱時,基底型別中的方法才會被衍生型別中的相同具名方法所隱藏。

如何修正違規

若要修正此規則的違規情形,請移除或重新命名此方法,或是變更參數簽章,因此方法不需隱藏基底方法。

隱藏警告的時機

請勿隱藏此規則的警告。

範例

下列範例會顯示違反此規則的方法。

using System;

namespace DesignLibrary
{
   class BaseType
   {
      internal void MethodOne(string inputOne, object inputTwo)
      {
         Console.WriteLine("Base: {0}, {1}", inputOne, inputTwo);
      }

      internal void MethodTwo(string inputOne, string inputTwo)
      {
         Console.WriteLine("Base: {0}, {1}", inputOne, inputTwo);
      }
   }

   class DerivedType : BaseType
   {
      internal void MethodOne(string inputOne, string inputTwo)
      {
         Console.WriteLine("Derived: {0}, {1}", inputOne, inputTwo);
      }

      // This method violates the rule.
      internal void MethodTwo(string inputOne, object inputTwo)
      {
         Console.WriteLine("Derived: {0}, {1}", inputOne, inputTwo);
      }
   }

   class Test
   {
      static void Main()
      {
         DerivedType derived = new DerivedType();

         // Calls DerivedType.MethodOne.
         derived.MethodOne("string1", "string2");

         // Calls BaseType.MethodOne.
         derived.MethodOne("string1", (object)"string2");

         // Both of these call DerivedType.MethodTwo.
         derived.MethodTwo("string1", "string2");
         derived.MethodTwo("string1", (object)"string2");
      }
   }
}