MethodBase.IsVirtual Właściwość

Definicja

Pobiera wartość wskazującą, czy metoda to virtual.

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

Wartość właściwości

true jeśli ta metoda to virtual; w przeciwnym razie false.

Implementuje

Przykłady

W poniższym przykładzie zostanie wyświetlona false wartość IsFinal, co może prowadzić do przesłonięcia MyMethod . Kod jest drukowany false , mimo że MyMethod nie jest oznaczony virtual i dlatego nie można go zastąpić.

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

Uwagi

Wirtualny element członkowski może odwoływać się do danych wystąpienia w klasie i musi być przywołyny za pośrednictwem wystąpienia klasy.

Aby ustalić, czy metoda jest przesłonięć, nie wystarczy sprawdzić, czy IsVirtual jest to true. Aby metoda można było zastąpić, musi mieć true wartość i IsFinal musi mieć wartość false. IsVirtual Na przykład metoda może nie być wirtualna, ale implementuje metodę interfejsu. Środowisko uruchomieniowe języka wspólnego wymaga, aby wszystkie metody implementujące elementy członkowskie interfejsu były oznaczone jako virtual; dlatego kompilator oznacza metodę virtual final. W związku z tym istnieją przypadki, w których metoda jest oznaczona jako , virtual ale nadal nie jest przesłonięć.

Aby ustalić z pewnością, czy metoda jest przesłonięć, użyj kodu takiego jak:

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

Jeśli IsVirtual parametr jest false lub IsFinal ma truewartość , nie można zastąpić metody .

Można określić, czy bieżąca metoda zastępuje metodę w klasie bazowej, wywołując metodę MethodInfo.GetBaseDefinition . Poniższy przykład implementuje metodę IsOverride , która to robi.

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

Dotyczy