MethodBase.IsVirtual 속성

정의

메서드가 virtual인지 여부를 나타내는 값을 가져옵니다.

public:
 property bool IsVirtual { bool get(); };
public bool IsVirtual { get; }
member this.IsVirtual : bool
Public ReadOnly Property IsVirtual As Boolean

속성 값

Boolean

이 메서드가 virtual이면 true이고, 그렇지 않으면 false입니다.

구현

예제

다음 예제에서는 재정의 false 할 수 있다고 생각 MyMethod 하게 될 수 있는 에 대해 IsFinal표시합니다. 코드가 false 표시되지 virtual 않았더라도 MyMethod 인쇄되므로 재정의할 수 없습니다.

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 하려면 .이어야 합니다 IsFinal true false. 예를 들어 메서드는 가상이 아니지만 인터페이스 메서드를 구현합니다. 공용 언어 런타임에서는 인터페이스 멤버를 구현하는 모든 메서드를 로 virtual표시해야 합니다. 따라서 컴파일러는 메서드 virtual final를 표시합니다. 따라서 메서드가 로 virtual 표시되지만 여전히 재정의할 수 없는 경우가 있습니다.

메서드를 재정의할 수 있는지 여부를 확실하게 설정하려면 다음과 같은 코드를 사용합니다.

if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)  
If MethodInfo.IsVirtual AndAlso Not MethodInfo.IsFinal Then  

false 경우 IsVirtual 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

적용 대상