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 namespace System;
using namespace System::Reflection;
// The base class Parent contains an overloaded method PrintCall.
//
public ref class Parent
{
public:
virtual void PrintCall()
{
Console::WriteLine("Parent's PrintCall()");
}
public:
virtual void PrintCall(int x)
{
Console::WriteLine("Parent's PrintCall({0})", x);
}
};
// The derived class Child hides one overload of the inherited
// method PrintCall.
//
public ref class Child : public Parent
{
public:
void PrintCall(int i) new
{
Console::WriteLine("Child's PrintCall({0})", i);
}
};
int main()
{
Child^ childInstance = gcnew Child();
// 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 PrintCall in the " +
"derived class Child ------");
Type^ t = childInstance->GetType();
for each(MethodInfo^ minfo in t->GetMethods())
{
if (minfo->Name == "PrintCall")
{
Console::WriteLine("Overload of PrintCall: {0}" +
" IsHideBySig = {1}, DeclaringType = {2}",
minfo, minfo->IsHideBySig, minfo->DeclaringType);
}
}
// The method PrintCall in the derived class hides one overload of the
// method in Parent. Contrast this with Visual Basic, which hides by
// name instead of by name and signature. In Visual Basic, the
// parameterless overload of PrintCall would be unavailable from Child.
//
Console::WriteLine(
"------ Call the overloads of PrintCall available in Child ------");
childInstance->PrintCall();
childInstance->PrintCall(42);
// If Child is cast to the base type Parent, both overloads of the
// shadowed method can be called.
//
Console::WriteLine(
"------ Call the shadowed overloads of PrintCall ------");
Parent^ parentInstance = childInstance;
parentInstance->PrintCall();
parentInstance->PrintCall(42);
}
/* This code example produces the following output:
------ List the overloads of PrintCall in the derived class Child ------
Overload of PrintCall: Void PrintCall(Int32) IsHideBySig = True, DeclaringType = Child
Overload of PrintCall: Void PrintCall() IsHideBySig = True, DeclaringType = Parent
Overload of PrintCall: Void PrintCall(Int32) IsHideBySig = True, DeclaringType = Parent
------ Call the overloads of PrintCall available in Child ------
Parent's PrintCall()
Child's PrintCall(42)
------ Call the shadowed overloads of PrintCall ------
Parent's PrintCall()
Parent's PrintCall(42)
*/
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 在使用 Visual Basic Shadows
修饰符声明的成员和true
使用 C# new
修饰符声明的成员上返回 false
。
警告
此属性不确定方法是否具有 特性 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
或 Shadows
声明的所有方法都具有 NewSlot 特性,但并非所有具有 特性 NewSlot 的方法都使用 new
或 Shadows
声明。