MethodBase.IsHideBySig 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,指出是否只有簽章完全一樣的同類成員隱藏於衍生類別中。
public:
property bool IsHideBySig { bool get(); };
public bool IsHideBySig { get; }
member this.IsHideBySig : bool
Public ReadOnly Property IsHideBySig As Boolean
屬性值
如果成員是根據簽章而隱藏,則為 true,否則為 false。
實作
範例
下列程式代碼範例包含具有多載方法的基類,以及隱藏其中一個多載的衍生類別。 在程式代碼範例的 Visual Basic 版本中, IsHideBySig 屬性會 false 針對衍生類別中的成員傳回 。 在程式代碼範例的 C# 版本中,屬性會 true 針對衍生類別中的成員傳回 。
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)
*/
Imports System.Reflection
' The base class B contains an overloaded method M.
'
Public Class B
Public Overridable Sub M()
Console.WriteLine("B's M()")
End Sub
Public Overridable Sub M(ByVal x As Integer)
Console.WriteLine("B's M({0})", x)
End Sub
End Class
' The derived class D hides the inherited method M.
'
Public Class D
Inherits B
Shadows Public Sub M(ByVal i As Integer)
Console.WriteLine("D's M({0})", i)
End Sub
End Class
Public Class Test
Public Shared Sub Main()
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.
'
Console.WriteLine("------ List the overloads of M in the derived class D ------")
Dim t As Type = dinst.GetType()
For Each minfo As MethodInfo In t.GetMethods()
If minfo.Name = "M" Then Console.WriteLine( _
"Overload of M: {0} IsHideBySig = {1}, DeclaringType = {2}", _
minfo, minfo.IsHideBySig, minfo.DeclaringType)
Next
' The method M in the derived class hides the method in B.
'
Console.WriteLine("------ Call the overloads of M available in D ------")
' 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.
'
Console.WriteLine("------ Call the shadowed overloads of M ------")
Dim binst As B = dinst
binst.M()
binst.M(42)
End Sub
End Class
' 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)
備註
使用 C# new 修飾詞或 Visual Basic Shadows 修飾詞宣告衍生類別中的成員時,它可以隱藏基類中相同名稱的成員。 C# 會依簽章隱藏基類成員。 也就是說,如果基類成員有多個多載,唯一隱藏的是具有相同簽章的多載。 相反地,Visual Basic 會隱藏所有基類多載。 因此, IsHideBySig 會 false 傳回以 Visual Basic Shadows 修飾詞宣告的成員,以及在 true 以 C# new 修飾詞宣告的成員上。
警告
這個屬性不會判斷方法 NewSlot 是否有 屬性。 使用 new 或 Shadows 修飾詞宣告的方法會有 NewSlot 屬性,但只有以 (宣告 new 的方法,也就是只有 C# 方法) 將 IsHideBySig 屬性設定為 true。 若要判斷方法 NewSlot 是否有 屬性,請使用類似下列的程序代碼: if ((myMethodInfo.Attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.NewSlot) 在 C# 或 If (myMethodInfo.Attributes And MethodAttributes.VtableLayoutMask) = MethodAttributes.NewSlot Visual Basic 中。 不過請注意,雖然使用 或 宣告new的所有方法都有 NewSlot 屬性,但並非所有具有 NewSlot 屬性的方法都會以 new 或 ShadowsShadows 宣告。