MethodBase.IsVirtual 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个值,该值指示方法是否为 virtual
。
public:
property bool IsVirtual { bool get(); };
public bool IsVirtual { get; }
member this.IsVirtual : bool
Public ReadOnly Property IsVirtual As Boolean
属性值
如果方法为 virtual
,则为 true
;否则为 false
。
实现
示例
下面的示例显示 false
了 IsFinal
,这可能会导致你认为 MyMethod
是可重写的。 false
即使 MyMethod
未标记,代码仍会打印 virtual
,因此不能重写。
using namespace System;
using namespace System::Reflection;
public ref class MyClass
{
public:
void MyMethod(){}
};
int main()
{
MethodBase^ m = MyClass::typeid->GetMethod( "MyMethod" );
Console::WriteLine( "The IsFinal property value of MyMethod is {0}.", m->IsFinal );
Console::WriteLine( "The IsVirtual property value of MyMethod is {0}.", m->IsVirtual );
}
using System;
using System.Reflection;
public class MyClass
{
public void MyMethod()
{
}
public static void Main()
{
MethodBase m = typeof(MyClass).GetMethod("MyMethod");
Console.WriteLine("The IsFinal property value of MyMethod is {0}.", m.IsFinal);
Console.WriteLine("The IsVirtual property value of MyMethod is {0}.", m.IsVirtual);
}
}
Imports System.Reflection
Public Class MyClass1
Public Sub MyMethod()
End Sub
Public Shared Sub Main()
Dim m As MethodBase = GetType(MyClass1).GetMethod("MyMethod")
Console.WriteLine("The IsFinal property value of MyMethod is {0}.", m.IsFinal)
Console.WriteLine("The IsVirtual property value of MyMethod is {0}.", m.IsVirtual)
End Sub
End Class
注解
虚拟成员可以引用类中的实例数据,并且必须通过 类的实例进行引用。
若要确定方法是否可重写,则检查 为 IsVirtual
是不够的 true
。 对于可重写的方法, 必须是 IsVirtual
true
, IsFinal 并且必须是 false
。 例如,方法可能是非虚拟的,但它实现了接口方法。 公共语言运行时要求实现接口成员的所有方法都必须标记为 virtual
;因此,编译器将方法 标记为 virtual final
。 因此,在某些情况下,方法标记为 , virtual
但仍不可重写。
若要确定某个方法是否可重写,请使用如下代码:
if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)
If MethodInfo.IsVirtual AndAlso Not MethodInfo.IsFinal Then
如果 IsVirtual
false
为 或 为 IsFinal
true
,则不能重写 方法。
可以通过调用 方法来确定当前方法是否重写基类中 MethodInfo.GetBaseDefinition 的方法。 下面的示例实现一 IsOverride
个实现此操作的方法。
using System;
using System.Reflection;
public class ReflectionUtilities
{
public static bool IsOverride(MethodInfo method)
{
return ! method.Equals(method.GetBaseDefinition());
}
}
public class Example
{
public static void Main()
{
MethodInfo equals = typeof(Int32).GetMethod("Equals",
new Type[] { typeof(Object) } );
Console.WriteLine("{0}.{1} is inherited: {2}",
equals.ReflectedType.Name, equals.Name,
ReflectionUtilities.IsOverride(equals));
equals = typeof(Object).GetMethod("Equals",
new Type[] { typeof(Object) } );
Console.WriteLine("{0}.{1} is inherited: {2}",
equals.ReflectedType.Name, equals.Name,
ReflectionUtilities.IsOverride(equals));
}
}
// The example displays the following output:
// Int32.Equals is inherited: True
// Object.Equals is inherited: False
Imports System.Reflection
Public Class ReflectionUtilities
Public Shared Function IsOverride(method As MethodInfo) As Boolean
Return Not method.Equals(method.GetBaseDefinition())
End Function
End Class
Module Example
Public Sub Main()
Dim equals As MethodInfo = GetType(Int32).GetMethod("Equals",
{ GetType(Object) } )
Console.WriteLine("{0}.{1} is inherited: {2}",
equals.ReflectedType.Name, equals.Name,
ReflectionUtilities.IsOverride(equals))
equals = GetType(Object).GetMethod("Equals", { GetType(Object) } )
Console.WriteLine("{0}.{1} is inherited: {2}",
equals.ReflectedType.Name, equals.Name,
ReflectionUtilities.IsOverride(equals))
End Sub
End Module
' The example displays the following output:
' Int32.Equals is inherited: True
' Object.Equals is inherited: False