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.

[Visual Basic]
Public MustOverride Function GetBaseDefinition() As MethodInfo
[C#]
public abstract MethodInfo GetBaseDefinition();
[C++]
public: virtual MethodInfo* GetBaseDefinition() = 0;
[JScript]
public abstract function GetBaseDefinition() : MethodInfo;

Return Value

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 (as in newslot as described in Type Members), 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.

Example

This code example demonstrates the behavior of GetBaseDefinition.

[Visual Basic] 
Imports System
Imports System.Reflection

Public Class GetBaseDef
    
    Public Shared Sub Main()
        Dim t As Type = GetType(B)
        Dim m As MethodInfo
        
        ' Print A Void B().
        m = t.GetMethod("B")
        Console.WriteLine(m.GetBaseDefinition().DeclaringType.ToString() + " " _
           + m.GetBaseDefinition().ToString())
        
        ' Print A Void C().
        m = t.GetMethod("C")
        Console.WriteLine(m.GetBaseDefinition().DeclaringType.ToString() + " " _
           + m.GetBaseDefinition().ToString())
        
        ' Print B Void D().
        m = t.GetMethod("D", BindingFlags.Public Or BindingFlags.Instance _
           Or BindingFlags.DeclaredOnly)
        Console.WriteLine(m.GetBaseDefinition().DeclaringType.ToString() + " " _
           + m.GetBaseDefinition().ToString())
    End Sub
    
End Class
 

Public Class A
    
    Public Overridable Sub B()
        Console.WriteLine("C")
    End Sub
    
    Public Overridable Sub C()
        Console.WriteLine("C")
    End Sub
    
    Public Overridable Sub D()
        Console.WriteLine("E")
    End Sub
End Class
 
Public Class B
    Inherits A
    
    Public Overrides Sub C()
        Console.WriteLine("C")
    End Sub
    
    Public Shadows Sub D()
        Console.WriteLine("D")
    End Sub
End Class

[C#] 
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"); }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;

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

public __gc class B : public A
{
public:
    void C() { Console::WriteLine(S"C"); }
    void D() { Console::WriteLine(S"D"); }
};

int main()
{
    Type* t = __typeof(B);
    MethodInfo* m;

    // Print A Void B().
    m = t->GetMethod(S"B");
    Console::WriteLine(S"{0} {1}", m->GetBaseDefinition()->DeclaringType, m->GetBaseDefinition());

    // Print A Void C().
    m = t->GetMethod(S"C");
    Console::WriteLine(S"{0} {1}", m->GetBaseDefinition()->DeclaringType, m->GetBaseDefinition());

    // Print B Void D().
    m = t->GetMethod(S"D",static_cast<BindingFlags>((BindingFlags::Public |
        BindingFlags::Instance |
        BindingFlags::DeclaredOnly)));
    Console::WriteLine(S"{0} {1}", m->GetBaseDefinition()->DeclaringType, m->GetBaseDefinition());
}

[JScript] 
import System;
import System.Reflection;

public class GetBaseDef {
    public static function Main() : void 
    {
    var t : Type = B;
    var m : MethodInfo;

    // 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());
    }
}

GetBaseDef.Main();

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

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

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

MethodInfo Class | MethodInfo Members | System.Reflection Namespace | Type Members

Show:
© 2014 Microsoft