MethodBase.IsVirtual プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
メソッドが virtualされているかどうかを示す値を取得します。
public:
property bool IsVirtual { bool get(); };
public bool IsVirtual { get; }
member this.IsVirtual : bool
Public ReadOnly Property IsVirtual As Boolean
プロパティ値
true このメソッドが virtualの場合は。それ以外の場合は false。
実装
例
次の例では、IsFinalのfalseを表示します。これは、MyMethodがオーバーライド可能であると考える可能性があります。
MyMethodがvirtualマークされていないため、オーバーライドできない場合でも、コードはfalseを出力します。
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