MethodBase.IsVirtual プロパティ

定義

メソッドが virtual であるかどうかを示す値を取得します。

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

プロパティ値

Boolean

このメソッドが virtual である場合は true。それ以外の場合は false

実装

次の例が表示false``IsFinalされ、オーバーライド可能であると考MyMethodえる可能性があります。 マークvirtualされていないのでオーバーライドできない場合MyMethodでも、コードは出力falseされます。

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次の値を指定する必要がありますfalse``trueIsFinal。 たとえば、メソッドは非仮想である可能性がありますが、インターフェイス メソッドを実装します。 共通言語ランタイムでは、インターフェイス メンバーを実装するすべてのメソッドを として virtualマークする必要があるため、コンパイラはメソッド virtual finalをマークします。 そのため、メソッドとしてマーク virtual されているが、引き続きオーバーライドできない場合があります。

メソッドがオーバーライド可能かどうかを確実に確立するには、次のようなコードを使用します。

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

あるfalse場合、またはIsFinalそれであるtrue場合IsVirtual、メソッドをオーバーライドすることはできません。

現在のメソッドが基底クラスのメソッドをオーバーライドするかどうかを確認するには、メソッドを 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

適用対象