MethodInfo.GetBaseDefinition Metodo

Definizione

Quando ne viene eseguito l'override in una classe derivata,, restituisce l'oggetto MethodInfo relativo al metodo presente nella classe base diretta o indiretta in cui il metodo rappresentato da questa istanza è stato inizialmente dichiarato.

public:
 abstract System::Reflection::MethodInfo ^ GetBaseDefinition();
public abstract System.Reflection.MethodInfo GetBaseDefinition ();
abstract member GetBaseDefinition : unit -> System.Reflection.MethodInfo
Public MustOverride Function GetBaseDefinition () As MethodInfo

Restituisce

Oggetto MethodInfo per la prima implementazione di questo metodo.

Implementazioni

Esempio

Nell'esempio GetBaseDefinition seguente viene illustrato il comportamento del metodo.

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
Imports System.Reflection

Interface Interf
   Function InterfaceImpl(n As Integer) As String
End Interface

Public Class BaseClass
   Public Overrides Function ToString() As String
      Return "Base"
   End Function

   Public Overridable Sub Method1()
      Console.WriteLine("Method1")
   End Sub

   Public Overridable Sub Method2()
      Console.WriteLine("Method2")
   End Sub

   Public Overridable Sub Method3()
      Console.WriteLine("Method3")
   End Sub
End Class

Public Class DerivedClass : Inherits BaseClass : Implements Interf
   Public Function InterfaceImpl(n As Integer) As String _
                   Implements Interf.InterfaceImpl
      Return n.ToString("N")
   End Function

   Public Overrides Sub Method2()
      Console.WriteLine("Derived.Method2")
   End Sub

   Public Shadows Sub Method3()
      Console.WriteLine("Derived.Method3")
   End Sub
End Class

Module Example
   Public Sub Main()
      Dim t As Type = GetType(DerivedClass)
      Dim m, mb As MethodInfo
      Dim methodNames() As String = { "ToString", "Equals",
                                      "InterfaceImpl", "Method1",
                                      "Method2", "Method3" }

      For Each 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)
      Next
   End Sub
End Module
' 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

Commenti

Il GetBaseDefinition metodo restituisce la prima definizione del metodo specificato nella gerarchia di classi. È possibile determinare il tipo in cui viene trovata la prima definizione del metodo recuperando il valore della DeclaringType proprietà nell'oggetto restituito MethodInfo .

Il GetBaseDefinition metodo si comporta come segue:

  • Se l'oggetto corrente MethodInfo rappresenta un'implementazione dell'interfaccia, il GetBaseDefinition metodo restituisce l'oggetto corrente MethodInfo .

  • Se l'oggetto corrente MethodInfo rappresenta un metodo che esegue l'override di una definizione virtuale in una classe di base, il GetBaseDefinition metodo restituisce un MethodInfo oggetto che rappresenta la definizione virtuale.

  • Se l'oggetto corrente MethodInfo rappresenta un metodo specificato con la new parola chiave in C# o la Shadows parola chiave in Visual Basic (come descritto in newslotCommon Type System), il GetBaseDefinition metodo restituisce l'oggetto correnteMethodInfo.

  • Se l'oggetto corrente rappresenta un metodo ereditato, ovvero il metodo corrente MethodInfo non fornisce la propria implementazione, il GetBaseDefinition metodo restituisce un MethodInfo oggetto che rappresenta il metodo più basso nella gerarchia di classi. Se, ad esempio, esegue l'override di , e esegue l'override Base.ToStringObject.ToStringGetBaseDefinition del Base.ToString metodo su un MethodInfo oggetto che rappresenta un MethodInfo oggetto che rappresenta Object.ToStringDerived.ToString .Derived.ToString

  • Se l'oggetto corrente MethodInfo rappresenta un metodo non presente in alcuna classe di base, il GetBaseDefinition metodo restituisce l'oggetto corrente MethodInfo .

È possibile determinare se il metodo corrente esegue l'override di un metodo in una classe di base chiamando il GetBaseDefinition metodo . Nell'esempio seguente viene implementato un IsOverride metodo che esegue questa operazione.

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
Imports System.Reflection

Public Class ReflectionUtilities
   Public Shared Function IsOverride(method As MethodInfo) As Boolean
      Return Not method.Equals(method.GetBaseDefinition())
   End Function
End Class

Module Example
   Public Sub Main()
      Dim equals As MethodInfo = GetType(Int32).GetMethod("Equals", 
                                         { GetType(Object) } )
      Console.WriteLine("{0}.{1} is inherited: {2}", 
                        equals.ReflectedType.Name, equals.Name,
                        ReflectionUtilities.IsOverride(equals))
      
      equals = GetType(Object).GetMethod("Equals", { GetType(Object) } )
      Console.WriteLine("{0}.{1} is inherited: {2}", 
                        equals.ReflectedType.Name, equals.Name,
                        ReflectionUtilities.IsOverride(equals))
   End Sub
End Module
' The example displays the following output:
'       Int32.Equals is inherited: True
'       Object.Equals is inherited: False

Per chiamare il GetBaseDefinition metodo:

  1. Ottenere un Type oggetto che rappresenta il tipo (la classe o la struttura) che contiene la proprietà . Se si usa un oggetto (un'istanza di un tipo), è possibile chiamare il GetType relativo metodo. In caso contrario, è possibile usare l'operatore C# o l'operatore GetType di Visual Basic, come illustrato nell'esempio.

  2. Ottenere un MethodInfo oggetto che rappresenta il metodo in cui si è interessati. A tale scopo, è possibile ottenere una matrice di tutti i metodi dal Type.GetMethods metodo e quindi eseguire l'iterazione degli elementi nella matrice oppure recuperare l'oggetto MethodInfo che rappresenta il metodo direttamente chiamando il metodo e specificando il Type.GetMethod(String) nome del metodo.

  3. Chiamare il GetBaseDefinition metodo per ottenere il valore dell'oggetto MethodInfo che rappresenta la definizione del metodo di base.

Si applica a

Vedi anche