Exporter (0) Imprimer
Développer tout
EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.

MethodBase.IsVirtual Property

Gets a value indicating whether the method is virtual.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

public bool IsVirtual { get; }
/** @property */
public final boolean get_IsVirtual ()

public final function get IsVirtual () : boolean

Not applicable.

Property Value

true if this method is virtual; otherwise, false.

A virtual member may reference instance data in a class and must be referenced through an instance of the class.

To determine if a method is overridable, it is not sufficient to check that IsVirtual is true. For a method to be overridable, IsVirtual must be true and IsFinal must be false. For example, a method might be non-virtual, but it implements an interface method. The common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method virtual final. So there are cases where a method is marked as virtual but is still not overridable.

To establish with certainty whether a method is overridable, use code such as this:

if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)

If IsVirtual is false or IsFinal is true, then the method cannot be overridden.

The following example displays false for IsFinal, which might lead you to think that MyMethod is overridable. The code prints false even though MyMethod is not marked virtual and thus cannot be overridden.

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);
    }
}

import System.*;
import System.Reflection.*;

public class MyClass
{   
    public void MyMethod()
    {
    } //MyMethod

    public static void main(String[] args)
    {
        MethodBase m = MyClass.class.ToType().GetMethod("MyMethod");
        Console.WriteLine("The IsFinal property value of MyMethod is {0}.",
            System.Convert.ToString( m.get_IsFinal()));
        Console.WriteLine("The IsVirtual property value of MyMethod is {0}.",
            System.Convert.ToString( m.get_IsVirtual()));
    } //main
} //MyClass

import System;
import System.Reflection;
 
 public class MyClass 
 {
 public function MyMethod() : void
   {
   }
 public static function Main() : void 
   {
    var m : MethodBase  = MyClass.GetMethod("MyMethod");
    Console.WriteLine(m.IsFinal);
   }
 }
 MyClass.Main();

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Ajouts de la communauté

AJOUTER
Afficher:
© 2014 Microsoft