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.

Implements

_MethodInfo.GetBaseDefinition()

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.

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# typeof operator or the Visual Basic GetType 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

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft