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.

The GetBaseDefinition method returns the first definition of the specified method in the class hierarchy. 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.

The GetBaseDefinition method behaves as follows:

  • If the current MethodInfo object represents an interface implementation, the GetBaseDefinition method returns the current MethodInfo object.

  • If the current MethodInfo object represents a method that overrides a virtual definition in a base class, the GetBaseDefinition method returns a MethodInfo object that represents the virtual definition.

  • If the current MethodInfo object represents a method that is specified with the new keyword in C# or the Shadows keyword in Visual Basic (as in newslot, as described in Common Type System), the GetBaseDefinition method returns the current MethodInfo object.

  • If the current MethodInfo object represents an inherited method (that is, the current method does not provide its own implementation), the GetBaseDefinition method returns a MethodInfo object that represents the lowest method in the class hierarchy. For example, if Base.ToString overrides Object.ToString, and Derived.ToString overrides Base.ToString, calling the GetBaseDefinition method on a MethodInfo object that represents Derived.ToString returns a MethodInfo object that represents Object.ToString.

  • If the current MethodInfo object represents a method that is not present in any base class, the GetBaseDefinition method returns the current MethodInfo object.

You can determine whether the current method overrides a method in a base class by calling the GetBaseDefinition method. 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

To call the GetBaseDefinition method:

  1. Get a Type object that represents the type (the class or structure) that contains the property. If you are working with an object (an instance of a type), you can call its GetType method. Otherwise, you can use the C# operator or the Visual Basic operator, as the example illustrates.

  2. Get a MethodInfo object that represents the method in which you're interested. 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. Call the GetBaseDefinition method to get the value of the MethodInfo object that represents the base method definition.

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

.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show: