共用方式為


MethodInfo.GetBaseDefinition Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public MustOverride Function GetBaseDefinition As MethodInfo
public abstract MethodInfo GetBaseDefinition()

Return Value

Type: System.Reflection.MethodInfo
A MethodInfo object for the first implementation of this method.

Remarks

GetBaseDefinition returns the first definition of the specified method in the class hierarchy.

If the method is declared on an interface, GetBaseDefinition returns the method.

If the method is defined in a base class, then GetBaseDefinition works as follows:

  • If a given method overrides a virtual definition in the base class, the virtual definition is returned.

  • If a given method is specified with the new keyword (that is, it hides inherited members with the same signature), then the given method is returned.

  • If the method is not defined in the type of the object on which GetBaseDefinition is called, the method definition highest in the class hierarchy is returned.

To get the GetBaseDefinition method, first get the class Type. From the Type, get the MethodInfo. From the MethodInfo, get the GetBaseDefinition.

Examples

This example demonstrates the behavior of GetBaseDefinition. Class B is derived from class A, which contains methods M1, M2, and M3. B inherits M1, overrides M2, and shadows M3. For each of these three methods of B, the example displays ReflectedType, DeclaringType, GetBaseDefinition, and the declaring type of the method returned by GetBaseDefinition.

Imports System.Reflection

Public Class Example

   Private Shared outputBlock As System.Windows.Controls.TextBlock

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Example.outputBlock = outputBlock

      Dim typeB As Type = GetType(B)

      DisplayMethod(typeB.GetMethod("M1"))
      DisplayMethod(typeB.GetMethod("M2"))
      DisplayMethod(typeB.GetMethod("M3"))

   End Sub

   Private Shared Sub DisplayMethod(ByVal m As MethodInfo)

      outputBlock.Text &= String.Format("Method {0}.{1}:" & vbLf, _
                                        m.ReflectedType.Name, m.Name)
      outputBlock.Text &= "   DeclaringType: " & m.DeclaringType.Name & vbLf

      Dim gbd As MethodInfo = m.GetBaseDefinition()
      outputBlock.Text &= String.Format("   GetBaseDefinition: {0}.{1}" & vbLf & vbLf, _
                                        gbd.DeclaringType.Name, gbd.Name)
   End Sub

End Class

Public Class A

   Public Overridable Sub M1()
      outputBlock.Text &= "A.M1" & vbCrLf
   End Sub

   Public Overridable Sub M2()
      outputBlock.Text &= "A.M2" & vbCrLf
   End Sub

   Public Overridable Sub M3()
      outputBlock.Text &= "A.M3" & vbCrLf
   End Sub

   Protected outputBlock As System.Windows.Controls.TextBlock
   Public Sub New(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Me.outputBlock = outputBlock
   End Sub
End Class

Public Class B
   Inherits A

   Public Overrides Sub M2()
      outputBlock.Text &= "B.M2" & vbCrLf
   End Sub

   Public Shadows Sub M3()
      outputBlock.Text &= "B.M3" & vbCrLf
   End Sub

   Public Sub New(ByVal outputBlock As System.Windows.Controls.TextBlock)
      MyBase.New(outputBlock)
   End Sub
End Class

' This example produces the following output:
'
'Method B.M1:
'   DeclaringType: A
'   GetBaseDefinition: A.M1
'
'Method B.M2:
'   DeclaringType: B
'   GetBaseDefinition: A.M2
'
'Method B.M3:
'   DeclaringType: B
'   GetBaseDefinition: B.M3
using System;
using System.Reflection;

public class Example
{
   private static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Example.outputBlock = outputBlock;

      Type typeB = typeof(B);

      DisplayMethod(typeB.GetMethod("M1"));
      DisplayMethod(typeB.GetMethod("M2"));
      DisplayMethod(typeB.GetMethod("M3")); 
   }

   private static void DisplayMethod(MethodInfo m)
   {

      outputBlock.Text += String.Format("Method {0}.{1}:\n", 
                                        m.ReflectedType.Name, m.Name);
      outputBlock.Text += "   DeclaringType: " + m.DeclaringType.Name + "\n";

      MethodInfo gbd = m.GetBaseDefinition();
      outputBlock.Text += String.Format("   GetBaseDefinition: {0}.{1}\n\n",
                                        gbd.DeclaringType.Name, gbd.Name);
   }
}

public class A
{

   public virtual void M1()
   {
      outputBlock.Text += "A.M1\n";
   }

   public virtual void M2()
   {
      outputBlock.Text += "A.M2\n";
   }

   public virtual void M3()
   {
      outputBlock.Text += "A.M3\n";
   }

   protected System.Windows.Controls.TextBlock outputBlock;
   public A(System.Windows.Controls.TextBlock outputBlock)
   {
      this.outputBlock = outputBlock;
   }
}

public class B:
   A

{
   public override void M2()
   {
      outputBlock.Text += "B.M2\n";
   }

   public new void M3()
   {
      outputBlock.Text += "B.M3\n";
   }

   public B(System.Windows.Controls.TextBlock outputBlock) : base(outputBlock) {}
}

/* This example produces the following output:

Method B.M1:
   DeclaringType: A
   GetBaseDefinition: A.M1

Method B.M2:
   DeclaringType: B
   GetBaseDefinition: A.M2

Method B.M3:
   DeclaringType: B
   GetBaseDefinition: B.M3
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.