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 オブジェクト。

実装

次の例では、 メソッドの動作を 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# の キーワードまたは Shadows Visual Basic の キーワード (Common Type System で説明されているようにnewslot) で指定されたnewメソッドを表す場合、メソッドはGetBaseDefinition現在MethodInfoのオブジェクトを返します。

  • 現在 MethodInfo のオブジェクトが継承されたメソッドを表す場合 (つまり、現在のメソッドは独自の実装を提供しません) GetBaseDefinition 、メソッドはクラス階層内で最も低いメソッドを表すオブジェクトを返 MethodInfo します。 たとえば、 が Object.ToStringオーバーライドされ、 Derived.ToString がオーバーライドBase.ToStringされた場合Base.ToString、 をGetBaseDefinition表す オブジェクトで メソッドをMethodInfo呼び出すと、 MethodInfo を表Derived.ToStringObject.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 表す オブジェクトの値を取得します。

適用対象

こちらもご覧ください