MethodInfo.GetBaseDefinition MethodInfo.GetBaseDefinition MethodInfo.GetBaseDefinition MethodInfo.GetBaseDefinition Method

定義

派生クラスによってオーバーライドされた場合、このインスタンスが表すメソッドが最初に宣言された直接または間接基本クラスで、そのメソッドの MethodInfo オブジェクトを返します。When overridden in a derived class, returns the MethodInfo object for the method on the direct or indirect base class in which the method represented by this instance was first declared.

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 オブジェクト。A MethodInfo object for the first implementation of this method.

実装

次の例での動作、GetBaseDefinitionメソッド。The following example demonstrates the behavior of the GetBaseDefinition method.

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
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メソッドは、クラス階層で指定されたメソッドの最初の定義を返します。The GetBaseDefinition method returns the first definition of the specified method in the class hierarchy. 値を取得することによって、メソッドの最初の定義を検索する型を指定できます、DeclaringTypeプロパティを返されたMethodInfoオブジェクト。You can determine the type on which the first definition of the method is found by retrieving the value of the DeclaringType property on the returned MethodInfo object.

GetBaseDefinitionメソッドは次のように動作します。The GetBaseDefinition method behaves as follows:

  • 場合、現在MethodInfoオブジェクトは、インターフェイスの実装を表します、GetBaseDefinitionメソッドは、現在を返してMethodInfoオブジェクト。If the current MethodInfo object represents an interface implementation, the GetBaseDefinition method returns the current MethodInfo object.

  • 場合、現在MethodInfoオブジェクトが仮想基底クラスの定義をオーバーライドするメソッドを表します、GetBaseDefinitionメソッドを返します。 をMethodInfo仮想の定義を表すオブジェクト。If the current MethodInfo object represents a method that overrides a virtual definition in a base class, the GetBaseDefinition method returns a MethodInfo object that represents the virtual definition.

  • 場合、現在MethodInfoオブジェクトで指定されているメソッドを表します、newキーワード (C#) またはShadowsVisual basic のキーワード (うにnewslot」の説明に従って、共通型システム)、 GetBaseDefinitionメソッドは、現在を返してMethodInfoオブジェクト。If the current MethodInfo object represents a method that is specified with the new keyword in C# or the Shadows keyword in Visual Basic (as in newslot, as described in Common Type System), the GetBaseDefinition method returns the current MethodInfo object.

  • 場合、現在MethodInfoオブジェクトは、継承されたメソッドを表します (つまり、現在のメソッドことはできません、独自の実装)、GetBaseDefinitionメソッドを返します。 をMethodInfoクラス階層の最下位のメソッドを表すオブジェクト。If the current MethodInfo object represents an inherited method (that is, the current method does not provide its own implementation), the GetBaseDefinition method returns a MethodInfo object that represents the lowest method in the class hierarchy. たとえば場合、Base.ToStringよりも優先されますObject.ToString、およびDerived.ToStringよりも優先されますBase.ToStringを呼び出すと、GetBaseDefinitionメソッドをMethodInfoを表すオブジェクトをDerived.ToStringを返します、MethodInfoを表すオブジェクトをObject.ToString.For example, if Base.ToString overrides Object.ToString, and Derived.ToString overrides Base.ToString, calling the GetBaseDefinition method on a MethodInfo object that represents Derived.ToString returns a MethodInfo object that represents Object.ToString.

  • 場合、現在MethodInfoオブジェクトが任意の基本クラスに存在しないメソッドを表す、GetBaseDefinitionメソッドは、現在を返してMethodInfoオブジェクト。If the current MethodInfo object represents a method that is not present in any base class, the GetBaseDefinition method returns the current MethodInfo object.

現在のメソッドが呼び出すことで基底クラス メソッドをオーバーライドするかどうかを指定できます、GetBaseDefinitionメソッド。You can determine whether the current method overrides a method in a base class by calling the GetBaseDefinition method. 次の例では、実装、IsOverrideこれを行うメソッドです。The following example implements an IsOverride method that does this.

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メソッド。To call the GetBaseDefinition method:

  1. 取得、Typeプロパティを含む型 (クラスまたは構造体) を表すオブジェクト。Get a Type object that represents the type (the class or structure) that contains the property. 呼び出すことができます (型のインスタンス) のオブジェクトを使用する場合、GetTypeメソッド。If you are working with an object (an instance of a type), you can call its GetType method. それ以外の場合、使用することができます、C#演算子や Visual Basic GetTypeオペレーターは、例として示します。Otherwise, you can use the C# operator or the Visual Basic GetType operator, as the example illustrates.

  2. 取得、MethodInfoで関心があるメソッドを表すオブジェクト。Get a MethodInfo object that represents the method in which you're interested. すべてのメソッドの配列を取得することによってこれを行う、Type.GetMethodsメソッドとするか、配列内の要素を反復処理し、取得できる、MethodInfo直接呼び出して、メソッドを表すオブジェクトをType.GetMethod(String)メソッドを指定する、メソッドの名前。You can do this by getting an array of all methods from the Type.GetMethods method and then iterating the elements in the array, or you can retrieve the MethodInfo object that represents the method directly by calling the Type.GetMethod(String) method and specifying the method name.

  3. 呼び出す、GetBaseDefinitionの値を取得するメソッド、MethodInfo基本メソッドの定義を表すオブジェクト。Call the GetBaseDefinition method to get the value of the MethodInfo object that represents the base method definition.

適用対象

こちらもご覧ください