MethodBase.IsVirtual MethodBase.IsVirtual MethodBase.IsVirtual MethodBase.IsVirtual Property

定义

获取一个值,该值指示方法是否为 virtualGets a value indicating whether the method is virtual.

public:
 property bool IsVirtual { bool get(); };
public bool IsVirtual { get; }
member this.IsVirtual : bool
Public ReadOnly Property IsVirtual As Boolean

属性值

如果方法为 virtual,则为 true;否则为 falsetrue if this method is virtual; otherwise, false.

实现

示例

下面的示例显示false IsFinal了,这可能会MyMethod导致你认为是可重写的。The following example displays false for IsFinal, which might lead you to think that MyMethod is overridable. 即使未标记falsevirtual代码仍会打印,因此不MyMethod能重写。The code prints false even though MyMethod is not marked virtual and thus cannot be overridden.

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

注解

虚拟成员可以引用类中的实例数据,并且必须通过类的实例进行引用。A virtual member may reference instance data in a class and must be referenced through an instance of the class.

若要确定某个方法是否可重写,只需检查IsVirtual是否true是。To determine if a method is overridable, it is not sufficient to check that IsVirtual is true. 要使方法可重写, IsVirtual必须是trueIsFinal并且必须false是。For a method to be overridable, IsVirtual must be true and IsFinal must be false. 例如,方法可能是非虚拟的,但它实现接口方法。For example, a method might be non-virtual, but it implements an interface method. 公共语言运行时要求实现接口成员的所有方法都必须标记为virtual; 因此,编译器将标记方法。 virtual finalThe common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method virtual final. 因此,在某些情况下,方法被标记virtual为,但仍不可重写。So there are cases where a method is marked as virtual but is still not overridable.

若要确定某个方法是否可重写,请使用如下所示的代码:To establish with certainty whether a method is overridable, use code such as this:

if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)  
If MethodInfo.IsVirtual AndAlso Not MethodInfo.IsFinal Then  

如果IsVirtualfalseIsFinal为,则不能重写此方法。trueIf IsVirtual is false or IsFinal is true, then the method cannot be overridden.

您可以通过调用MethodInfo.GetBaseDefinition方法来确定当前方法是否重写基类中的方法。You can determine whether the current method overrides a method in a base class by calling the MethodInfo.GetBaseDefinition method. 下面的示例实现实现IsOverride此功能的方法。The following example implements an IsOverride method that does this.

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

适用于