Export (0) Print
Expand All

MethodInfo.GetBaseDefinition Method

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)

public abstract MethodInfo GetBaseDefinition()

Return Value

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

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.

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.


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
 */


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

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

Community Additions

ADD
Show:
© 2014 Microsoft