MethodBase.IsVirtual 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值指出方法是否為 virtual
。
public:
property bool IsVirtual { bool get(); };
public bool IsVirtual { get; }
member this.IsVirtual : bool
Public ReadOnly Property IsVirtual As Boolean
屬性值
如果這個方法為 virtual
則為 true
;否則為 false
。
實作
範例
下列範例顯示的 false
IsFinal
,可能會讓您認為可覆 MyMethod
寫。 即使未標記程式碼,程式碼仍會列印 false
MyMethod
virtual
,因此無法覆寫。
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
必須是 true
,而且 IsFinal 必須是 false
。 例如,方法可能是非虛擬的,但它會實作為介面方法。 Common language runtime 要求所有實介面成員的方法都必須標記為 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