MethodInfo.GetBaseDefinition 메서드

정의

파생 클래스에서 재정의되는 경우 이 인스턴스가 나타내는 메서드가 처음 선언된 직접 또는 간접 기본 클래스의 메서드에 대해 MethodInfo 개체를 반환합니다.

public:
 abstract System::Reflection::MethodInfo ^ GetBaseDefinition();
public abstract System.Reflection.MethodInfo GetBaseDefinition ();
abstract member GetBaseDefinition : unit -> System.Reflection.MethodInfo
Public MustOverride Function GetBaseDefinition () As MethodInfo

반환

MethodInfo

이 메서드의 첫 번째 구현에 대한 MethodInfo 개체입니다.

구현

예제

다음 예제에서는 동작을 보여 줍니다.는 GetBaseDefinition 메서드.

using System;
using System.Reflection;

interface Interf
{
   string InterfaceImpl(int n);
}

public class BaseClass
{
   public override string ToString()
   {
      return "Base";
   }

   public virtual void Method1()
   {
      Console.WriteLine("Method1");
   }

   public virtual void Method2()
   {
      Console.WriteLine("Method2");
   }

   public virtual void Method3()
   {
      Console.WriteLine("Method3");
   }
}

public class DerivedClass : BaseClass, Interf
{
   public string InterfaceImpl(int n)
   {
      return n.ToString("N");
   }

   public override void Method2()
   {
      Console.WriteLine("Derived.Method2");
   }

   public new void Method3()
   {
      Console.WriteLine("Derived.Method3");
   }
}

public class Example
{
   public static void Main()
   {
      Type t = typeof(DerivedClass);
      MethodInfo m, mb;
      string[] methodNames = { "ToString", "Equals", "InterfaceImpl",
                               "Method1", "Method2", "Method3" };

      foreach (var methodName in methodNames) {
         m = t.GetMethod(methodName);
         mb = m.GetBaseDefinition();
         Console.WriteLine("{0}.{1} --> {2}.{3}", m.ReflectedType.Name,
                           m.Name, mb.ReflectedType.Name, mb.Name);
      }
   }
}
// The example displays the following output:
//       DerivedClass.ToString --> Object.ToString
//       DerivedClass.Equals --> Object.Equals
//       DerivedClass.InterfaceImpl --> DerivedClass.InterfaceImpl
//       DerivedClass.Method1 --> BaseClass.Method1
//       DerivedClass.Method2 --> BaseClass.Method2
//       DerivedClass.Method3 --> DerivedClass.Method3
Imports System.Reflection

Interface Interf
   Function InterfaceImpl(n As Integer) As String
End Interface

Public Class BaseClass
   Public Overrides Function ToString() As String
      Return "Base"
   End Function

   Public Overridable Sub Method1()
      Console.WriteLine("Method1")
   End Sub

   Public Overridable Sub Method2()
      Console.WriteLine("Method2")
   End Sub

   Public Overridable Sub Method3()
      Console.WriteLine("Method3")
   End Sub
End Class

Public Class DerivedClass : Inherits BaseClass : Implements Interf
   Public Function InterfaceImpl(n As Integer) As String _
                   Implements Interf.InterfaceImpl
      Return n.ToString("N")
   End Function

   Public Overrides Sub Method2()
      Console.WriteLine("Derived.Method2")
   End Sub

   Public Shadows Sub Method3()
      Console.WriteLine("Derived.Method3")
   End Sub
End Class

Module Example
   Public Sub Main()
      Dim t As Type = GetType(DerivedClass)
      Dim m, mb As MethodInfo
      Dim methodNames() As String = { "ToString", "Equals",
                                      "InterfaceImpl", "Method1",
                                      "Method2", "Method3" }

      For Each methodName In methodNames
         m = t.GetMethod(methodName)
         mb = m.GetBaseDefinition()
         Console.WriteLine("{0}.{1} --> {2}.{3}", m.ReflectedType.Name,
                           m.Name, mb.ReflectedType.Name, mb.Name)
      Next
   End Sub
End Module
' The example displays the following output:
'       DerivedClass.ToString --> Object.ToString
'       DerivedClass.Equals --> Object.Equals
'       DerivedClass.InterfaceImpl --> DerivedClass.InterfaceImpl
'       DerivedClass.Method1 --> BaseClass.Method1
'       DerivedClass.Method2 --> BaseClass.Method2
'       DerivedClass.Method3 --> DerivedClass.Method3

설명

이 메서드는 GetBaseDefinition 클래스 계층 구조에서 지정된 메서드의 첫 번째 정의를 반환합니다. 반환 MethodInfo 된 개체의 속성 값을 검색하여 메서드의 DeclaringType 첫 번째 정의를 찾을 형식을 확인할 수 있습니다.

메서드는 GetBaseDefinition 다음과 같이 동작합니다.

  • 현재 MethodInfo 개체가 인터페이스 구현을 나타내는 경우 메서드는 GetBaseDefinition 현재 MethodInfo 개체를 반환합니다.

  • 현재 MethodInfo 개체가 기본 클래스의 가상 정의를 재정의하는 메서드를 나타내는 경우 메서드는 GetBaseDefinition 가상 정의를 나타내는 개체를 반환 MethodInfo 합니다.

  • 현재 MethodInfo 개체가 C#의 키워드 또는 new Shadows Visual Basic 키워드로 지정된 메서드를 나타내는 경우(Common Type Systemnewslot설명된 대로) GetBaseDefinition 메서드는 현재 MethodInfo 개체를 반환합니다.

  • 현재 MethodInfo 개체가 상속된 메서드를 나타내는 경우(즉, 현재 메서드가 자체 구현을 제공하지 않음) GetBaseDefinition 메서드는 클래스 계층 구조에서 가장 낮은 메서드를 나타내는 개체를 반환 MethodInfo 합니다. 예를 들어 재정의하고 재정의하는 경우 Base.ToString 나타내는 Derived.ToString 개체에서 메서드를 MethodInfo 호출 GetBaseDefinition 하면 나타내는 개체가 반환 MethodInfo 됩니다Object.ToString.Base.ToString``Derived.ToString Object.ToString

  • 현재 MethodInfo 개체가 기본 클래스에 없는 메서드를 나타내는 경우 메서드는 GetBaseDefinition 현재 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

메서드를 호출하려면 다음을 수행 GetBaseDefinition 합니다.

  1. Type 속성을 포함하는 형식(클래스 또는 구조체)을 나타내는 개체를 가져옵니다. 개체(형식의 인스턴스)로 작업하는 경우 해당 메서드를 호출할 GetType 수 있습니다. 그렇지 않으면 예제와 같이 C# 연산자 또는 Visual Basic GetType 연산자를 사용할 수 있습니다.

  2. 관심 있는 MethodInfo 메서드를 나타내는 개체를 가져옵니다. 메서드에서 Type.GetMethods 모든 메서드의 배열을 가져오고 배열의 요소를 반복하거나 메서드를 호출 Type.GetMethod(String) 하고 메서드 이름을 지정하여 메서드를 직접 나타내는 개체를 검색 MethodInfo 할 수 있습니다.

  3. 메서드를 GetBaseDefinition 호출하여 기본 메서드 정의를 나타내는 개체의 MethodInfo 값을 가져옵니다.

적용 대상

추가 정보