MethodInfo.GetBaseDefinition 方法

定义

当在派生类中被重写时,为直接或间接的基类(用该实例表示的方法首先在此类中声明)上的方法返回 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.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:

您可以通过调用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方法获取所有方法的数组,然后循环访问数组中的元素,也可以通过调用Type.GetMethod(String)方法直接MethodInfo检索表示方法的对象,并指定方法名。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. 调用方法可获取表示基方法定义的MethodInfo对象的值。 GetBaseDefinitionCall the GetBaseDefinition method to get the value of the MethodInfo object that represents the base method definition.

适用于

另请参阅