MethodInfo.GetBaseDefinition Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
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.
Assembly: mscorlib (in mscorlib.dll)
Return Value
Type: System.Reflection.MethodInfoA 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.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |
Imports System.Reflection Public Class Example Private Shared outputBlock As System.Windows.Controls.TextBlock Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) Example.outputBlock = outputBlock Dim typeB As Type = GetType(B) DisplayMethod(typeB.GetMethod("M1")) DisplayMethod(typeB.GetMethod("M2")) DisplayMethod(typeB.GetMethod("M3")) End Sub Private Shared Sub DisplayMethod(ByVal m As MethodInfo) outputBlock.Text &= String.Format("Method {0}.{1}:" & vbLf, _ m.ReflectedType.Name, m.Name) outputBlock.Text &= " DeclaringType: " & m.DeclaringType.Name & vbLf Dim gbd As MethodInfo = m.GetBaseDefinition() outputBlock.Text &= String.Format(" GetBaseDefinition: {0}.{1}" & vbLf & vbLf, _ gbd.DeclaringType.Name, gbd.Name) End Sub End Class Public Class A Public Overridable Sub M1() outputBlock.Text &= "A.M1" & vbCrLf End Sub Public Overridable Sub M2() outputBlock.Text &= "A.M2" & vbCrLf End Sub Public Overridable Sub M3() outputBlock.Text &= "A.M3" & vbCrLf End Sub Protected outputBlock As System.Windows.Controls.TextBlock Public Sub New(ByVal outputBlock As System.Windows.Controls.TextBlock) Me.outputBlock = outputBlock End Sub End Class Public Class B Inherits A Public Overrides Sub M2() outputBlock.Text &= "B.M2" & vbCrLf End Sub Public Shadows Sub M3() outputBlock.Text &= "B.M3" & vbCrLf End Sub Public Sub New(ByVal outputBlock As System.Windows.Controls.TextBlock) MyBase.New(outputBlock) End Sub End Class ' 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
Note: