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
實作
範例
以下範例顯示 false , IsFinal這可能會讓你以為 是 MyMethod 可覆蓋的。 即使沒有標記virtual,程式碼仍會列印falseMyMethod,因此無法被覆寫。
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
備註
虛擬成員可以參考類別中的實例資料,且必須透過該類別的實例來引用。
要判斷一個方法是否可覆寫,僅檢查 是 IsVirtualtrue。 要讓一個方法可覆寫,必須IsVirtual是,且IsFinal必須是falsetrue。 例如,一個方法可能是非虛擬的,但它實作了一個介面方法。 通用語言執行時要求所有實作介面成員的方法都必須標記為 virtual;因此,編譯器會標記該方法 virtual final。 所以有些方法被標記為 , virtual 但仍然無法覆寫。
為了確定某個方法是否可覆寫,可以使用以下程式碼:
if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)
If MethodInfo.IsVirtual AndAlso Not MethodInfo.IsFinal Then
若 IsVirtual 或 falseIsFinal , 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