Compartir a través de


MethodBase.IsHideBySig Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a value that indicates whether only a member of the same kind with exactly the same signature is hidden in the derived class.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public ReadOnly Property IsHideBySig As Boolean
public bool IsHideBySig { get; }

Property Value

Type: System.Boolean
true if the member is hidden by signature; otherwise, false.

Remarks

When a member in a derived class is declared with the C# new modifier or the Visual Basic Shadows modifier, it can hide a member of the same name in the base class. C# hides base class members by signature. That is, if the base class member has multiple overloads, the only one that is hidden is the one with the identical signature. By contrast, Visual Basic hides all the base class overloads. Therefore, IsHideBySig returns false on a member that is declared with the Visual Basic Shadows modifier, and true on a member that is declared with the C# new modifier.

Examples

The following example contains a base class with an overloaded method, and a derived class that hides one of the overloads. In the Visual Basic version of the example, the IsHideBySig property returns false for the member in the derived class. In the C# version of the example, the property returns true for the member in the derived class.

Imports System.Reflection

' The base class B contains an overloaded method M.
'
Public Class B
   Public Overridable Sub M()
      outputBlock.Text &= "B's M()" & vbCrLf
   End Sub
   Public Overridable Sub M(ByVal x As Integer)
      outputBlock.Text &= String.Format("B's M({0})", x) & vbCrLf
   End Sub

   Friend Shared outputBlock As System.Windows.Controls.TextBlock
End Class

' The derived class D hides the inherited method M.
'
Public Class D
   Inherits B
   Public Shadows Sub M(ByVal i As Integer)
      outputBlock.Text &= String.Format("D's M({0})", i) & vbCrLf
   End Sub

End Class

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      B.outputBlock = outputBlock

      Dim dinst As New D()
      ' In Visual Basic, the method in the derived class hides by
      ' name, rather than by signature.  Thus, although a list of all the 
      ' overloads of M shows three overloads, only one can be called from
      ' class D.  
      '
      outputBlock.Text &= "------ List the overloads of M in the derived class D ------" & vbCrLf
      Dim t As Type = dinst.GetType()
      For Each minfo As MethodInfo In t.GetMethods()
         If minfo.Name = "M" Then outputBlock.Text &= String.Format( _
             "Overload of M: {0}  IsHideBySig = {1}, DeclaringType = {2}", _
             minfo, minfo.IsHideBySig, minfo.DeclaringType) & vbCrLf
      Next

      ' The method M in the derived class hides the method in B.
      '
      outputBlock.Text &= "------ Call the overloads of M available in D ------" & vbCrLf
      ' The following line causes a compile error, because both overloads
      ' in the base class are hidden.  Contrast this with C#, where only 
      ' one of the overloads of B would be hidden.
      'dinst.M()
      dinst.M(42)

      ' If D is cast to the base type B, both overloads of the 
      ' shadowed method can be called.
      '
      outputBlock.Text &= "------ Call the shadowed overloads of M ------" & vbCrLf
      Dim binst As B = dinst
      binst.M()
      binst.M(42)
   End Sub 'Main
End Class 'Test

' This code example produces the following output:
' ------ List the overloads of M in the derived class D ------
' Overload of M: Void M(Int32)  IsHideBySig = False, DeclaringType = B
' Overload of M: Void M()  IsHideBySig = False, DeclaringType = B
' Overload of M: Void M(Int32)  IsHideBySig = False, DeclaringType = D
' ------ Call the overloads of M available in D ------
' D's M(42)
' ------ Call the shadowed overloads of M ------
' B's M()
' B's M(42)
using System;
using System.Reflection;

// The base class B contains an overloaded method M.
//
public class B
{
   public virtual void M()
   {
      outputBlock.Text += "B's M()" + "\n";
   }
   public virtual void M(int x)
   {
      outputBlock.Text += String.Format("B's M({0})", x) + "\n";
   }

   internal static System.Windows.Controls.TextBlock outputBlock;
}

// The derived class D hides one overload of the inherited 
// method M.
//
public class D :
    B
{
   new public void M(int i)
   {
      outputBlock.Text += String.Format("D's M({0})", i) + "\n";
   }
}

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      B.outputBlock = outputBlock;

      D dinst = new D();
      // In C#, the method in the derived class hides by name and by
      // signature, so the overload in the derived class hides only one
      // of the overloads in the base class.
      //
      outputBlock.Text += "------ List the overloads of M in the derived class D ------" + "\n";
      Type t = dinst.GetType();
      foreach (MethodInfo minfo in t.GetMethods())
      {
         if (minfo.Name == "M") { outputBlock.Text += String.Format("Overload of M: {0}  IsHideBySig = {1}, DeclaringType = {2}", minfo, minfo.IsHideBySig, minfo.DeclaringType) + "\n"; }
      }

      // The method M in the derived class hides one overload of the 
      // method in B.  Contrast this with Visual Basic, which hides by
      // name instead of by name and signature.  In Visual Basic, the
      // parameterless overload of M would be unavailable from D.
      //
      outputBlock.Text += "------ Call the overloads of M available in D ------" + "\n";
      dinst.M();
      dinst.M(42);

      // If D is cast to the base type B, both overloads of the 
      // shadowed method can be called.
      //
      outputBlock.Text += "------ Call the shadowed overloads of M ------" + "\n";
      B binst = dinst;
      binst.M();
      binst.M(42);
   } //Main
} //Test

/* This code example produces the following output:

------ List the overloads of M in the derived class D ------
Overload of M: Void M(Int32)  IsHideBySig = True, DeclaringType = B
Overload of M: Void M()  IsHideBySig = True, DeclaringType = B
Overload of M: Void M(Int32)  IsHideBySig = True, DeclaringType = D
------ Call the overloads of M available in D ------
B's M()
D's M(42)
------ Call the shadowed overloads of M ------
B's M()
B's M(42)
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.