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 的行为如下所示:

可以通过调用 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

适用于

另请参阅