This topic has not yet been rated - Rate this topic

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.

Implements

_MethodInfo.GetBaseDefinition()

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 (as in newslot as described in Common Type System), 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 code example demonstrates the behavior of GetBaseDefinition.


using System;
using System.Reflection;

public class GetBaseDef {
    public static void Main(String[] args)
    {
    Type t = typeof(B);
    MethodInfo m;

    // Print A Void B().
    m = t.GetMethod("B");
    Console.WriteLine(m.GetBaseDefinition().DeclaringType + " " + m.GetBaseDefinition());

    // Print A Void C().
    m = t.GetMethod("C");
    Console.WriteLine(m.GetBaseDefinition().DeclaringType + " " + m.GetBaseDefinition());

    // Print B Void D().
    m = t.GetMethod("D", (BindingFlags.Public |
                  BindingFlags.Instance |
                  BindingFlags.DeclaredOnly));
    Console.WriteLine(m.GetBaseDefinition().DeclaringType + " " + m.GetBaseDefinition());
    }

}

public class A
{
    public virtual void B() { Console.WriteLine("C"); }
    public virtual void C() { Console.WriteLine("C"); }
    public virtual void D() { Console.WriteLine("E"); }
}

public class B : A
{
    public override void C() { Console.WriteLine("C"); }
    public new void D()  { Console.WriteLine("D"); }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Detecting if a virtual method introduced by a base type has been overridden in any derived type
$0Something many programmers do not realize about virtual methods, is that they can avoid consequential or collateral overhead when a virtual method has not been overridden in any derived type comprising an instance.$0 $0$0 $0 $0In that case, where the virtual method is intended to provide notification to a derived type, not only can the call to the virtual method be avoided, but any associated overhead related to calling the method can be avoided as well.$0 $0$0 $0 $0Here is an example showing how a base type that introduces a virtual method, can$0 $0use GetBaseDefinition() to determine if any derived type comprising an instance$0 $0has provided an override of that method:$0 $0$0 $0 $0$0 $0 $0$0 $0
namespace Namespace1
{
  public static class Class1
  {
    void Main()
    {
      new B();
      new C();
      new D();
    }
  }

  public class A
  {
    protected A()
    {
      Delegate d = ((Func<int,string>) this.Convert);
      bool flag = d.Method != d.Method.GetBaseDefinition();
      Console.WriteLine("new {0}(): A.Convert() {1} overridden",
        this.GetType().Name, flag ? "is" : "is not" );
    }
    
    protected virtual string Convert( int i )
    {
      return i.ToString();
    }
  }

  public class B : A
  {
    protected override string Convert( int i )
    {
      return i.ToString();
    }
  }

  public class C : A
  {
  }

  public class D : C
  {
    protected override string Convert( int i )
    {
      return i.ToString();
    }
  }
}
$0
$0$0 $0