英語で読む

次の方法で共有


MethodBase.IsHideBySig プロパティ

定義

派生クラスで、正確に同じシグネチャを持つ同じ種類のメンバーだけが隠しメンバーになるかどうかを示す値を取得します。

C#
public bool IsHideBySig { get; }

プロパティ値

メンバーがシグネチャで隠される場合は true。それ以外の場合は false

実装

次のコード例には、オーバーロードされたメソッドを持つ基底クラスと、オーバーロードの 1 つを非表示にする派生クラスが含まれています。 Visual Basic バージョンのコード例では、 プロパティは IsHideBySig 派生クラスのメンバーを返 false します。 コード サンプルの C# バージョンでは、 プロパティは派生クラスのメンバーを返 true します。

C#
using System;
using System.Reflection;

// The base class B contains an overloaded method M.
//
public class B
{
    public virtual void M()
    {
        Console.WriteLine("B's M()");
    }
    public virtual void M(int x)
    {
        Console.WriteLine("B's M({0})", x);
    }
}

// The derived class D hides one overload of the inherited 
// method M.
//
public class D:
    B
{
    new public void M(int i)
    {
        Console.WriteLine("D's M({0})", i);
    }
}

public class Test
{
    public static void Main()
    {
        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.
        //
        Console.WriteLine("------ List the overloads of M in the derived class D ------");
        Type t = dinst.GetType();
        foreach( MethodInfo minfo in t.GetMethods() )
        {
            if (minfo.Name=="M") {Console.WriteLine("Overload of M: {0}  IsHideBySig = {1}, DeclaringType = {2}", minfo, minfo.IsHideBySig, minfo.DeclaringType);}
        }

        // 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.
        //
        Console.WriteLine("------ Call the overloads of M available in D ------");
        dinst.M();
        dinst.M(42);
        
        // If D is cast to the base type B, both overloads of the 
        // shadowed method can be called.
        //
        Console.WriteLine("------ Call the shadowed overloads of M ------");
        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)
*/

注釈

派生クラスのメンバーが C# new 修飾子または Visual Basic Shadows 修飾子を使用して宣言されている場合、基底クラスの同じ名前のメンバーを非表示にすることができます。 C# では、シグネチャによって基底クラスのメンバーが非表示になります。 つまり、基底クラスメンバーに複数のオーバーロードがある場合、非表示になっているのは同じシグネチャを持つ唯一のオーバーロードです。 これに対し、Visual Basic ではすべての基本クラスのオーバーロードが非表示になります。 したがって、 IsHideBySig は Visual Basic Shadows 修飾子で宣言されたメンバーとtrue、C# new 修飾子で宣言されたメンバーに 対して false を返します。

警告

このプロパティは、メソッド NewSlot に 属性があるかどうかを判断しません。 または 修飾子をnew使用して宣言されたメソッドは 属性をNewSlot持ちますが、 で宣言されたnewメソッド (つまり、C# メソッドのみ) のみが プロパティを IsHideBySigtrue設定Shadowsします。 メソッド NewSlot に 属性があるかどうかを判断するには、次のようなコードを使用します。 if ((myMethodInfo.Attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.NewSlot) C# または If (myMethodInfo.Attributes And MethodAttributes.VtableLayoutMask) = MethodAttributes.NewSlot Visual Basic の場合。 ただし、 または で宣言newShadowsされたすべてのメソッドには 属性がありますが、 属性を持NewSlotNewSlotすべてのメソッドが または Shadowsnew宣言されているわけではありません。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0