MethodBase.IsVirtual Tulajdonság
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Egy értéket kap, amely jelzi, hogy a metódus .virtual
public:
property bool IsVirtual { bool get(); };
public bool IsVirtual { get; }
member this.IsVirtual : bool
Public ReadOnly Property IsVirtual As Boolean
Tulajdonság értéke
trueha ez a virtualmódszer ; egyébként. false
Megvalósítás
Példák
Az alábbi példa a következőhöz falsejelenik megIsFinal, amely alapján azt gondolhatja, hogy MyMethod ez felülírható. A kód akkor is nyomtat, false ha MyMethod nincs megjelölve virtual , ezért nem bírálható felül.
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
Megjegyzések
A virtuális tag hivatkozhat egy osztály példányadataira, és az osztály egy példányán keresztül kell hivatkoznia.
Annak megállapításához, hogy egy metódus felülírható-e, nem elegendő annak ellenőrzése IsVirtualtrue. Ahhoz, hogy egy metódus felülírható legyen, IsVirtual meg kell és trueIsFinal kell is lenniefalse. Előfordulhat például, hogy egy metódus nem virtuális, de implementál egy interfészmetódust. A közös nyelvi futtatókörnyezethez az interfésztagokat végrehajtó összes metódust meg kell jelölni virtual; ezért a fordító jelöli a metódust virtual final. Vannak tehát olyan esetek, amikor egy metódust a rendszer megjelöl virtual , de még mindig nem bírálható felül.
Annak megállapításához, hogy egy metódus felülírható-e, használja a következőhöz hasonló kódot:
if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)
If MethodInfo.IsVirtual AndAlso Not MethodInfo.IsFinal Then
Ha IsVirtual igen false , IsFinaltrueakkor a metódus nem bírálható felül.
A metódus meghívásával meghatározhatja, hogy az aktuális metódus felülbírál-e egy metódust egy MethodInfo.GetBaseDefinition alaposztályban. Az alábbi példa egy IsOverride metódust implementál, amely ezt teszi.
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