MethodBase.Invoke Método

Definición

Invoca el método o constructor que refleja esta instancia de MethodInfo.

Sobrecargas

Invoke(Object, Object[])

Invoca el método o constructor representado por la instancia actual, utilizando los parámetros especificados.

Invoke(Object, BindingFlags, Binder, Object[], CultureInfo)

Cuando se reemplaza en una clase derivada, invoca el método o constructor reflejado con los parámetros especificados.

Invoke(Object, Object[])

Source:
MethodBase.cs
Source:
MethodBase.cs
Source:
MethodBase.cs

Invoca el método o constructor representado por la instancia actual, utilizando los parámetros especificados.

public:
 virtual System::Object ^ Invoke(System::Object ^ obj, cli::array <System::Object ^> ^ parameters);
public:
 System::Object ^ Invoke(System::Object ^ obj, cli::array <System::Object ^> ^ parameters);
public virtual object Invoke (object obj, object[] parameters);
public object? Invoke (object? obj, object?[]? parameters);
public object Invoke (object obj, object[] parameters);
abstract member Invoke : obj * obj[] -> obj
override this.Invoke : obj * obj[] -> obj
member this.Invoke : obj * obj[] -> obj
Public Overridable Function Invoke (obj As Object, parameters As Object()) As Object
Public Function Invoke (obj As Object, parameters As Object()) As Object

Parámetros

obj
Object

Objeto en el que se va a invocar al método o constructor. Si el método es estático, se omite este argumento. Si un constructor es estático, este argumento debe ser null o una instancia de la clase que define el constructor.

parameters
Object[]

Lista de argumentos del método o constructor invocado. Esta es una matriz de objetos con el mismo número, orden y tipo que los parámetros del método o constructor al que se van a invocar. Si no hay ningún parámetro, parameters deberá ser null.

Si el método o constructor representado por esta instancia tiene un parámetro ref (ByRef en Visual Basic), no es necesario ningún atributo especial para ese parámetro para invocar el método o constructor mediante esta función. Cualquier objeto de esta matriz que no se inicialice explícitamente con un valor contendrá el valor predeterminado de este tipo de objeto. Para los elementos de tipo de referencia, este valor es null. Para los elementos de tipo de valor, el valor predeterminado es 0, 0,0 o false, dependiendo del tipo de elemento específico.

Devoluciones

Objeto que contiene el valor devuelto del método invocado, o null en caso de un constructor.

Implementaciones

Excepciones

El parámetro obj es null y el método no es estático.

o bien

La clase de obj no declara ni hereda el método.

o bien

Se invoca un constructor estático, y obj no es ni null ni una instancia de la clase que declaró el constructor.

Nota: En .NET para aplicaciones de la Tienda Windows o la biblioteca de clases portable, detecte Exception en su lugar.

Los elementos de la matriz parameters no coinciden con la firma del método o constructor que esta instancia refleja.

El método o constructor invocado produce una excepción.

o bien

La instancia actual es un DynamicMethod que contiene código no comprobable. Vea la sección "Comprobación" en Comentarios para DynamicMethod.

La matriz parameters no tiene el número correcto de argumentos.

El autor de la llamada no tiene permiso para ejecutar el método o constructor representado por la instancia actual.

Nota: En .NET para aplicaciones de la Tienda Windows o la Biblioteca de clases portable, detecte la excepción de clase base, MemberAccessException, en su lugar.

El tipo que declara el método es un tipo genérico abierto. Es decir, la propiedad ContainsGenericParameters devuelve true para el tipo declarador.

La instancia actual es un MethodBuilder.

Ejemplos

En el ejemplo de código siguiente se muestra la búsqueda de métodos dinámicos mediante la reflexión. Tenga en cuenta que no puede usar el MethodInfo objeto de la clase base para invocar el método invalidado en la clase derivada, ya que el enlace en tiempo de ejecución no puede resolver invalidaciones.

using namespace System;
using namespace System::Reflection;

public ref class MagicClass
{
private:
    int magicBaseValue;

public:
    MagicClass()
    {
        magicBaseValue = 9;
    }

    int ItsMagic(int preMagic)
    {
        return preMagic * magicBaseValue;
    }
};

public ref class TestMethodInfo
{
public:
    static void Main()
    {
        // Get the constructor and create an instance of MagicClass

        Type^ magicType = Type::GetType("MagicClass");
        ConstructorInfo^ magicConstructor = magicType->GetConstructor(Type::EmptyTypes);
        Object^ magicClassObject = magicConstructor->Invoke(gcnew array<Object^>(0));

        // Get the ItsMagic method and invoke with a parameter value of 100

        MethodInfo^ magicMethod = magicType->GetMethod("ItsMagic");
        Object^ magicValue = magicMethod->Invoke(magicClassObject, gcnew array<Object^>(1){100});

        Console::WriteLine("MethodInfo.Invoke() Example\n");
        Console::WriteLine("MagicClass.ItsMagic() returned: {0}", magicValue);
    }
};

int main()
{
    TestMethodInfo::Main();
}

// The example program gives the following output:
//
// MethodInfo.Invoke() Example
//
// MagicClass.ItsMagic() returned: 900
using System;
using System.Reflection;

public class MagicClass
{
    private int magicBaseValue;

    public MagicClass()
    {
        magicBaseValue = 9;
    }

    public int ItsMagic(int preMagic)
    {
        return preMagic * magicBaseValue;
    }
}

public class TestMethodInfo
{
    public static void Main()
    {
        // Get the constructor and create an instance of MagicClass

        Type magicType = Type.GetType("MagicClass");
        ConstructorInfo magicConstructor = magicType.GetConstructor(Type.EmptyTypes);
        object magicClassObject = magicConstructor.Invoke(new object[]{});

        // Get the ItsMagic method and invoke with a parameter value of 100

        MethodInfo magicMethod = magicType.GetMethod("ItsMagic");
        object magicValue = magicMethod.Invoke(magicClassObject, new object[]{100});

        Console.WriteLine("MethodInfo.Invoke() Example\n");
        Console.WriteLine("MagicClass.ItsMagic() returned: {0}", magicValue);
    }
}

// The example program gives the following output:
//
// MethodInfo.Invoke() Example
//
// MagicClass.ItsMagic() returned: 900
Imports System.Reflection

Public Class MagicClass
    Private magicBaseValue As Integer

    Public Sub New()
        magicBaseValue = 9
    End Sub

    Public Function ItsMagic(preMagic As Integer) As Integer
        Return preMagic * magicBaseValue
    End Function
End Class

Public Class TestMethodInfo
    Public Shared Sub Main()
        ' Get the constructor and create an instance of MagicClass

        Dim magicType As Type = Type.GetType("MagicClass")
        Dim magicConstructor As ConstructorInfo = magicType.GetConstructor(Type.EmptyTypes)
        Dim magicClassObject As Object = magicConstructor.Invoke(New Object(){})

        ' Get the ItsMagic method and invoke with a parameter value of 100

        Dim magicMethod As MethodInfo = magicType.GetMethod("ItsMagic")
        Dim magicValue As Object = magicMethod.Invoke(magicClassObject, New Object(){100})

        Console.WriteLine("MethodInfo.Invoke() Example" + Environment.NewLine)
        Console.WriteLine("MagicClass.ItsMagic() returned: {0}", magicValue)
    End Sub
End Class

' The example program gives the following output:
'
' MethodInfo.Invoke() Example
'
' MagicClass.ItsMagic() returned: 900

Comentarios

Se trata de un método de conveniencia que llama a la sobrecarga del Invoke(Object, BindingFlags, Binder, Object[], CultureInfo) método, pasando Default para invokeAttr y null para binder y culture.

Si el método invocado produce una excepción, el Exception.GetBaseException método devuelve la excepción de origen.

Para invocar un método estático mediante su MethodInfo objeto , pase null para obj.

Nota

Si se usa esta sobrecarga de método para invocar un constructor de instancia, se reinicializa el objeto proporcionado para obj ; es decir, se ejecutan todos los inicializadores de instancia. El valor devuelto es null. Si se invoca un constructor de clase, la clase se reinicializa; es decir, se ejecutan todos los inicializadores de clase. El valor devuelto es null.

Nota

A partir de .NET Framework 2.0, este método se puede usar para acceder a miembros no públicos si se ha concedido ReflectionPermission al autor de la llamada con la ReflectionPermissionFlag.RestrictedMemberAccess marca y si el conjunto de concesión de los miembros no públicos está restringido al conjunto de concesión del autor de la llamada o a un subconjunto de ellos. (Consulte Consideraciones de seguridad para la reflexión). Para usar esta funcionalidad, la aplicación debe tener como destino .NET Framework 3.5 o posterior.

Si un parámetro del método reflejado es un tipo de valor y el argumento correspondiente de parameters es null, el tiempo de ejecución pasa una instancia inicializada de cero del tipo de valor.

Consulte también

Se aplica a

Invoke(Object, BindingFlags, Binder, Object[], CultureInfo)

Source:
MethodBase.cs
Source:
MethodBase.cs
Source:
MethodBase.cs

Cuando se reemplaza en una clase derivada, invoca el método o constructor reflejado con los parámetros especificados.

public:
 abstract System::Object ^ Invoke(System::Object ^ obj, System::Reflection::BindingFlags invokeAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ parameters, System::Globalization::CultureInfo ^ culture);
public abstract object? Invoke (object? obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? parameters, System.Globalization.CultureInfo? culture);
public abstract object Invoke (object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture);
abstract member Invoke : obj * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo -> obj
Public MustOverride Function Invoke (obj As Object, invokeAttr As BindingFlags, binder As Binder, parameters As Object(), culture As CultureInfo) As Object

Parámetros

obj
Object

Objeto en el que se va a invocar al método o constructor. Si el método es estático, se omite este argumento. Si un constructor es estático, este argumento debe ser null o una instancia de la clase que define el constructor.

invokeAttr
BindingFlags

Una máscara de bits que es una combinación de 0 o más marcas de bits de BindingFlags.

binder
Binder

Objeto que habilita el enlace, la conversión de tipos de argumentos, las llamadas a miembros y la recuperación de objetos MemberInfo mediante reflexión. Si binder es null, se usa el enlazador predeterminado.

parameters
Object[]

Lista de argumentos del método o constructor invocado. Esta es una matriz de objetos con el mismo número, orden y tipo que los parámetros del método o constructor al que se van a invocar. Si no hay ningún parámetro, debe ser null.

Si el método o constructor que representa esta instancia adopta un parámetro ByRef, éste no necesita ningún atributo especial para que pueda invocar el método o constructor utilizando esta función. Cualquier objeto de esta matriz que no se inicialice explícitamente con un valor contendrá el valor predeterminado de este tipo de objeto. Para los elementos de tipo de referencia, este valor es null. Para los elementos de tipo de valor, este valor es 0, 0.0 o false, en función del tipo de elemento específico.

culture
CultureInfo

Instancia de CultureInfo usada para regir la conversión de tipos. Si es null, se utiliza CultureInfo para el subproceso actual. (Esto es necesario para convertir una cadena que representa 1000 en un valor Double, por ejemplo, dado que 1000 se representa de maneras diferentes según la referencia cultural).

Devoluciones

Un tipo Object que contiene el valor devuelto del método invocado, o un objeto null en el caso de un constructor, o null si el tipo de valor devuelto del método es void. Antes de llamar al método o constructor, Invoke comprueba si el usuario tiene permiso de acceso y si los parámetros son válidos.

Implementaciones

Excepciones

El parámetro obj es null y el método no es estático.

o bien

La clase de obj no declara ni hereda el método.

o bien

Se invoca un constructor estático, y obj no es ni null ni una instancia de la clase que declaró el constructor.

El tipo del parámetro parameters no coincide con la firma del método o constructor que esta instancia refleja.

La matriz parameters no tiene el número correcto de argumentos.

El método o constructor invocado produce una excepción.

El autor de la llamada no tiene permiso para ejecutar el método o constructor representado por la instancia actual.

El tipo que declara el método es un tipo genérico abierto. Es decir, la propiedad ContainsGenericParameters devuelve true para el tipo declarador.

Ejemplos

En el ejemplo siguiente se muestran todos los miembros de la System.Reflection.Binder clase mediante una sobrecarga de Type.InvokeMember. El método CanConvertFrom privado busca tipos compatibles para un tipo determinado. Para obtener otro ejemplo de invocación de miembros en un escenario de enlace personalizado, consulte Carga dinámica y uso de tipos.

using namespace System;
using namespace System::Reflection;
using namespace System::Globalization;
using namespace System::Runtime::InteropServices;
public ref class MyBinder: public Binder
{
public:
   MyBinder()
      : Binder()
   {}

private:
   ref class BinderState
   {
   public:
      array<Object^>^args;
   };

public:
   virtual FieldInfo^ BindToField( BindingFlags bindingAttr, array<FieldInfo^>^match, Object^ value, CultureInfo^ culture ) override
   {
      if ( match == nullptr )
            throw gcnew ArgumentNullException( "match" );

      // Get a field for which the value parameter can be converted to the specified field type.
      for ( int i = 0; i < match->Length; i++ )
         if ( ChangeType( value, match[ i ]->FieldType, culture ) != nullptr )
                  return match[ i ];

      return nullptr;
   }

   virtual MethodBase^ BindToMethod( BindingFlags bindingAttr, array<MethodBase^>^match, array<Object^>^%args, array<ParameterModifier>^ modifiers, CultureInfo^ culture, array<String^>^names, [Out]Object^% state ) override
   {
      // Store the arguments to the method in a state Object*.
      BinderState^ myBinderState = gcnew BinderState;
      array<Object^>^arguments = gcnew array<Object^>(args->Length);
      args->CopyTo( arguments, 0 );
      myBinderState->args = arguments;
      state = myBinderState;
      if ( match == nullptr )
            throw gcnew ArgumentNullException;

      // Find a method that has the same parameters as those of the args parameter.
      for ( int i = 0; i < match->Length; i++ )
      {
         // Count the number of parameters that match.
         int count = 0;
         array<ParameterInfo^>^parameters = match[ i ]->GetParameters();

         // Go on to the next method if the number of parameters do not match.
         if ( args->Length != parameters->Length )
                  continue;

         // Match each of the parameters that the user expects the method to have.
         for ( int j = 0; j < args->Length; j++ )
         {
            // If the names parameter is not 0, then reorder args.
            if ( names != nullptr )
            {
               if ( names->Length != args->Length )
                              throw gcnew ArgumentException( "names and args must have the same number of elements." );

               for ( int k = 0; k < names->Length; k++ )
                  if ( String::Compare( parameters[ j ]->Name, names[ k ] ) == 0 )
                                    args[ j ] = myBinderState->args[ k ];
            }

            // Determine whether the types specified by the user can be converted to the parameter type.
            if ( ChangeType( args[ j ], parameters[ j ]->ParameterType, culture ) != nullptr )
                        count += 1;
            else
                        break;
         }
         if ( count == args->Length )
                  return match[ i ];
      }
      return nullptr;
   }

   virtual Object^ ChangeType( Object^ value, Type^ myChangeType, CultureInfo^ culture ) override
   {
      // Determine whether the value parameter can be converted to a value of type myType.
      if ( CanConvertFrom( value->GetType(), myChangeType ) )
         // Return the converted Object*.
         return Convert::ChangeType( value, myChangeType ); 
      else
         return nullptr;
   }

   virtual void ReorderArgumentArray( array<Object^>^%args, Object^ state ) override
   {
      // Return the args that had been reordered by BindToMethod.
      (safe_cast<BinderState^>(state))->args->CopyTo( args, 0 );
   }

   virtual MethodBase^ SelectMethod( BindingFlags bindingAttr, array<MethodBase^>^match, array<Type^>^types, array<ParameterModifier>^ modifiers ) override
   {
      if ( match == nullptr )
            throw gcnew ArgumentNullException( "match" );

      for ( int i = 0; i < match->Length; i++ )
      {
         // Count the number of parameters that match.
         int count = 0;
         array<ParameterInfo^>^parameters = match[ i ]->GetParameters();

         // Go on to the next method if the number of parameters do not match.
         if ( types->Length != parameters->Length )
                  continue;

         // Match each of the parameters that the user expects the method to have.
         for ( int j = 0; j < types->Length; j++ )
         {
            // Determine whether the types specified by the user can be converted to parameter type.
            if ( CanConvertFrom( types[ j ], parameters[ j ]->ParameterType ) )
                        count += 1;
            else
                        break;
         }
         // Determine whether the method has been found.
         if ( count == types->Length )
                  return match[ i ];
      }
      return nullptr;
   }

   virtual PropertyInfo^ SelectProperty( BindingFlags bindingAttr, array<PropertyInfo^>^match, Type^ returnType, array<Type^>^indexes, array<ParameterModifier>^ modifiers ) override
   {
      if ( match == nullptr )
            throw gcnew ArgumentNullException( "match" );

      for ( int i = 0; i < match->Length; i++ )
      {
         // Count the number of indexes that match.
         int count = 0;
         array<ParameterInfo^>^parameters = match[ i ]->GetIndexParameters();

         // Go on to the next property if the number of indexes do not match.
         if ( indexes->Length != parameters->Length )
                  continue;

         // Match each of the indexes that the user expects the property to have.
         for ( int j = 0; j < indexes->Length; j++ )
            // Determine whether the types specified by the user can be converted to index type.
            if ( CanConvertFrom( indexes[ j ], parameters[ j ]->ParameterType ) )
                        count += 1;
            else
                        break;

         // Determine whether the property has been found.
         if ( count == indexes->Length )
         {
            // Determine whether the return type can be converted to the properties type.
            if ( CanConvertFrom( returnType, match[ i ]->PropertyType ) )
                  return match[ i ];
            else
                  continue;
         }
      }
      return nullptr;
   }

private:

   // Determines whether type1 can be converted to type2. Check only for primitive types.
   bool CanConvertFrom( Type^ type1, Type^ type2 )
   {
      if ( type1->IsPrimitive && type2->IsPrimitive )
      {
         TypeCode typeCode1 = Type::GetTypeCode( type1 );
         TypeCode typeCode2 = Type::GetTypeCode( type2 );

         // If both type1 and type2 have the same type, return true.
         if ( typeCode1 == typeCode2 )
                  return true;

         // Possible conversions from Char follow.
         if ( typeCode1 == TypeCode::Char )
         {
            switch ( typeCode2 )
            {
               case TypeCode::UInt16:
                  return true;

               case TypeCode::UInt32:
                  return true;

               case TypeCode::Int32:
                  return true;

               case TypeCode::UInt64:
                  return true;

               case TypeCode::Int64:
                  return true;

               case TypeCode::Single:
                  return true;

               case TypeCode::Double:
                  return true;

               default:
                  return false;
            }
         }

         // Possible conversions from Byte follow.
         if ( typeCode1 == TypeCode::Byte )
         {
            switch ( typeCode2 )
            {
               case TypeCode::Char:
                  return true;

               case TypeCode::UInt16:
                  return true;

               case TypeCode::Int16:
                  return true;

               case TypeCode::UInt32:
                  return true;

               case TypeCode::Int32:
                  return true;

               case TypeCode::UInt64:
                  return true;

               case TypeCode::Int64:
                  return true;

               case TypeCode::Single:
                  return true;

               case TypeCode::Double:
                  return true;

               default:
                  return false;
            }
         }

         // Possible conversions from SByte follow.
         if ( typeCode1 == TypeCode::SByte )
         {
            switch ( typeCode2 )
            {
               case TypeCode::Int16:
                  return true;

               case TypeCode::Int32:
                  return true;

               case TypeCode::Int64:
                  return true;

               case TypeCode::Single:
                  return true;

               case TypeCode::Double:
                  return true;

               default:
                  return false;
            }
         }

         // Possible conversions from UInt16 follow.
         if ( typeCode1 == TypeCode::UInt16 )
         {
            switch ( typeCode2 )
            {
               case TypeCode::UInt32:
                  return true;

               case TypeCode::Int32:
                  return true;

               case TypeCode::UInt64:
                  return true;

               case TypeCode::Int64:
                  return true;

               case TypeCode::Single:
                  return true;

               case TypeCode::Double:
                  return true;

               default:
                  return false;
            }
         }

         // Possible conversions from Int16 follow.
         if ( typeCode1 == TypeCode::Int16 )
         {
            switch ( typeCode2 )
            {
               case TypeCode::Int32:
                  return true;

               case TypeCode::Int64:
                  return true;

               case TypeCode::Single:
                  return true;

               case TypeCode::Double:
                  return true;

               default:
                  return false;
            }
         }

         // Possible conversions from UInt32 follow.
         if ( typeCode1 == TypeCode::UInt32 )
         {
            switch ( typeCode2 )
            {
               case TypeCode::UInt64:
                  return true;

               case TypeCode::Int64:
                  return true;

               case TypeCode::Single:
                  return true;

               case TypeCode::Double:
                  return true;

               default:
                  return false;
            }
         }

         // Possible conversions from Int32 follow.
         if ( typeCode1 == TypeCode::Int32 )
         {
            switch ( typeCode2 )
            {
               case TypeCode::Int64:
                  return true;

               case TypeCode::Single:
                  return true;

               case TypeCode::Double:
                  return true;

               default:
                  return false;
            }
         }

         // Possible conversions from UInt64 follow.
         if ( typeCode1 == TypeCode::UInt64 )
         {
            switch ( typeCode2 )
            {
               case TypeCode::Single:
                  return true;

               case TypeCode::Double:
                  return true;

               default:
                  return false;
            }
         }

         // Possible conversions from Int64 follow.
         if ( typeCode1 == TypeCode::Int64 )
         {
            switch ( typeCode2 )
            {
               case TypeCode::Single:
                  return true;

               case TypeCode::Double:
                  return true;

               default:
                  return false;
            }
         }

         // Possible conversions from Single follow.
         if ( typeCode1 == TypeCode::Single )
         {
            switch ( typeCode2 )
            {
               case TypeCode::Double:
                  return true;

               default:
                  return false;
            }
         }
      }

      return false;
   }

};

public ref class MyClass1
{
public:
   short myFieldB;
   int myFieldA;
   void MyMethod( long i, char k )
   {
      Console::WriteLine( "\nThis is MyMethod(long i, char k)" );
   }

   void MyMethod( long i, long j )
   {
      Console::WriteLine( "\nThis is MyMethod(long i, long j)" );
   }
};

int main()
{
   // Get the type of MyClass1.
   Type^ myType = MyClass1::typeid;

   // Get the instance of MyClass1.
   MyClass1^ myInstance = gcnew MyClass1;
   Console::WriteLine( "\nDisplaying the results of using the MyBinder binder.\n" );

   // Get the method information for MyMethod.
   array<Type^>^types = {short::typeid,short::typeid};
   MethodInfo^ myMethod = myType->GetMethod( "MyMethod", static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance), gcnew MyBinder, types, nullptr );
   Console::WriteLine( myMethod );

   // Invoke MyMethod.
   array<Object^>^obj = {32,32};
   myMethod->Invoke( myInstance, BindingFlags::InvokeMethod, gcnew MyBinder, obj, CultureInfo::CurrentCulture );
}
using System;
using System.Reflection;
using System.Globalization;

public class MyBinder : Binder
{
    public MyBinder() : base()
    {
    }
    private class BinderState
    {
        public object[] args;
    }
    public override FieldInfo BindToField(
        BindingFlags bindingAttr,
        FieldInfo[] match,
        object value,
        CultureInfo culture
        )
    {
        if(match == null)
            throw new ArgumentNullException("match");
        // Get a field for which the value parameter can be converted to the specified field type.
        for(int i = 0; i < match.Length; i++)
            if(ChangeType(value, match[i].FieldType, culture) != null)
                return match[i];
        return null;
    }
    public override MethodBase BindToMethod(
        BindingFlags bindingAttr,
        MethodBase[] match,
        ref object[] args,
        ParameterModifier[] modifiers,
        CultureInfo culture,
        string[] names,
        out object state
        )
    {
        // Store the arguments to the method in a state object.
        BinderState myBinderState = new BinderState();
        object[] arguments = new Object[args.Length];
        args.CopyTo(arguments, 0);
        myBinderState.args = arguments;
        state = myBinderState;
        if(match == null)
            throw new ArgumentNullException();
        // Find a method that has the same parameters as those of the args parameter.
        for(int i = 0; i < match.Length; i++)
        {
            // Count the number of parameters that match.
            int count = 0;
            ParameterInfo[] parameters = match[i].GetParameters();
            // Go on to the next method if the number of parameters do not match.
            if(args.Length != parameters.Length)
                continue;
            // Match each of the parameters that the user expects the method to have.
            for(int j = 0; j < args.Length; j++)
            {
                // If the names parameter is not null, then reorder args.
                if(names != null)
                {
                    if(names.Length != args.Length)
                        throw new ArgumentException("names and args must have the same number of elements.");
                    for(int k = 0; k < names.Length; k++)
                        if(String.Compare(parameters[j].Name, names[k].ToString()) == 0)
                            args[j] = myBinderState.args[k];
                }
                // Determine whether the types specified by the user can be converted to the parameter type.
                if(ChangeType(args[j], parameters[j].ParameterType, culture) != null)
                    count += 1;
                else
                    break;
            }
            // Determine whether the method has been found.
            if(count == args.Length)
                return match[i];
        }
        return null;
    }
    public override object ChangeType(
        object value,
        Type myChangeType,
        CultureInfo culture
        )
    {
        // Determine whether the value parameter can be converted to a value of type myType.
        if(CanConvertFrom(value.GetType(), myChangeType))
            // Return the converted object.
            return Convert.ChangeType(value, myChangeType);
        else
            // Return null.
            return null;
    }
    public override void ReorderArgumentArray(
        ref object[] args,
        object state
        )
    {
        // Return the args that had been reordered by BindToMethod.
        ((BinderState)state).args.CopyTo(args, 0);
    }
    public override MethodBase SelectMethod(
        BindingFlags bindingAttr,
        MethodBase[] match,
        Type[] types,
        ParameterModifier[] modifiers
        )
    {
        if(match == null)
            throw new ArgumentNullException("match");
        for(int i = 0; i < match.Length; i++)
        {
            // Count the number of parameters that match.
            int count = 0;
            ParameterInfo[] parameters = match[i].GetParameters();
            // Go on to the next method if the number of parameters do not match.
            if(types.Length != parameters.Length)
                continue;
            // Match each of the parameters that the user expects the method to have.
            for(int j = 0; j < types.Length; j++)
                // Determine whether the types specified by the user can be converted to parameter type.
                if(CanConvertFrom(types[j], parameters[j].ParameterType))
                    count += 1;
                else
                    break;
            // Determine whether the method has been found.
            if(count == types.Length)
                return match[i];
        }
        return null;
    }
    public override PropertyInfo SelectProperty(
        BindingFlags bindingAttr,
        PropertyInfo[] match,
        Type returnType,
        Type[] indexes,
        ParameterModifier[] modifiers
        )
    {
        if(match == null)
            throw new ArgumentNullException("match");
        for(int i = 0; i < match.Length; i++)
        {
            // Count the number of indexes that match.
            int count = 0;
            ParameterInfo[] parameters = match[i].GetIndexParameters();
            // Go on to the next property if the number of indexes do not match.
            if(indexes.Length != parameters.Length)
                continue;
            // Match each of the indexes that the user expects the property to have.
            for(int j = 0; j < indexes.Length; j++)
                // Determine whether the types specified by the user can be converted to index type.
                if(CanConvertFrom(indexes[j], parameters[j].ParameterType))
                    count += 1;
                else
                    break;
            // Determine whether the property has been found.
            if(count == indexes.Length)
                // Determine whether the return type can be converted to the properties type.
                if(CanConvertFrom(returnType, match[i].PropertyType))
                    return match[i];
                else
                    continue;
        }
        return null;
    }
    // Determines whether type1 can be converted to type2. Check only for primitive types.
    private bool CanConvertFrom(Type type1, Type type2)
    {
        if(type1.IsPrimitive && type2.IsPrimitive)
        {
            TypeCode typeCode1 = Type.GetTypeCode(type1);
            TypeCode typeCode2 = Type.GetTypeCode(type2);
            // If both type1 and type2 have the same type, return true.
            if(typeCode1 == typeCode2)
                return true;
            // Possible conversions from Char follow.
            if(typeCode1 == TypeCode.Char)
                switch(typeCode2)
                {
                    case TypeCode.UInt16 : return true;
                    case TypeCode.UInt32 : return true;
                    case TypeCode.Int32  : return true;
                    case TypeCode.UInt64 : return true;
                    case TypeCode.Int64  : return true;
                    case TypeCode.Single : return true;
                    case TypeCode.Double : return true;
                    default              : return false;
                }
            // Possible conversions from Byte follow.
            if(typeCode1 == TypeCode.Byte)
                switch(typeCode2)
                {
                    case TypeCode.Char   : return true;
                    case TypeCode.UInt16 : return true;
                    case TypeCode.Int16  : return true;
                    case TypeCode.UInt32 : return true;
                    case TypeCode.Int32  : return true;
                    case TypeCode.UInt64 : return true;
                    case TypeCode.Int64  : return true;
                    case TypeCode.Single : return true;
                    case TypeCode.Double : return true;
                    default              : return false;
                }
            // Possible conversions from SByte follow.
            if(typeCode1 == TypeCode.SByte)
                switch(typeCode2)
                {
                    case TypeCode.Int16  : return true;
                    case TypeCode.Int32  : return true;
                    case TypeCode.Int64  : return true;
                    case TypeCode.Single : return true;
                    case TypeCode.Double : return true;
                    default              : return false;
                }
            // Possible conversions from UInt16 follow.
            if(typeCode1 == TypeCode.UInt16)
                switch(typeCode2)
                {
                    case TypeCode.UInt32 : return true;
                    case TypeCode.Int32  : return true;
                    case TypeCode.UInt64 : return true;
                    case TypeCode.Int64  : return true;
                    case TypeCode.Single : return true;
                    case TypeCode.Double : return true;
                    default              : return false;
                }
            // Possible conversions from Int16 follow.
            if(typeCode1 == TypeCode.Int16)
                switch(typeCode2)
                {
                    case TypeCode.Int32  : return true;
                    case TypeCode.Int64  : return true;
                    case TypeCode.Single : return true;
                    case TypeCode.Double : return true;
                    default              : return false;
                }
            // Possible conversions from UInt32 follow.
            if(typeCode1 == TypeCode.UInt32)
                switch(typeCode2)
                {
                    case TypeCode.UInt64 : return true;
                    case TypeCode.Int64  : return true;
                    case TypeCode.Single : return true;
                    case TypeCode.Double : return true;
                    default              : return false;
                }
            // Possible conversions from Int32 follow.
            if(typeCode1 == TypeCode.Int32)
                switch(typeCode2)
                {
                    case TypeCode.Int64  : return true;
                    case TypeCode.Single : return true;
                    case TypeCode.Double : return true;
                    default              : return false;
                }
            // Possible conversions from UInt64 follow.
            if(typeCode1 == TypeCode.UInt64)
                switch(typeCode2)
                {
                    case TypeCode.Single : return true;
                    case TypeCode.Double : return true;
                    default              : return false;
                }
            // Possible conversions from Int64 follow.
            if(typeCode1 == TypeCode.Int64)
                switch(typeCode2)
                {
                    case TypeCode.Single : return true;
                    case TypeCode.Double : return true;
                    default              : return false;
                }
            // Possible conversions from Single follow.
            if(typeCode1 == TypeCode.Single)
                switch(typeCode2)
                {
                    case TypeCode.Double : return true;
                    default              : return false;
                }
        }
        return false;
    }
}
public class MyClass1
{
    public short myFieldB;
    public int myFieldA;
    public void MyMethod(long i, char k)
    {
        Console.WriteLine("\nThis is MyMethod(long i, char k)");
    }
    public void MyMethod(long i, long j)
    {
        Console.WriteLine("\nThis is MyMethod(long i, long j)");
    }
}
public class Binder_Example
{
    public static void Main()
    {
        // Get the type of MyClass1.
        Type myType = typeof(MyClass1);
        // Get the instance of MyClass1.
        MyClass1 myInstance = new MyClass1();
        Console.WriteLine("\nDisplaying the results of using the MyBinder binder.\n");
        // Get the method information for MyMethod.
        MethodInfo myMethod = myType.GetMethod("MyMethod", BindingFlags.Public | BindingFlags.Instance,
            new MyBinder(), new Type[] {typeof(short), typeof(short)}, null);
        Console.WriteLine(myMethod);
        // Invoke MyMethod.
        myMethod.Invoke(myInstance, BindingFlags.InvokeMethod, new MyBinder(), new Object[] {(int)32, (int)32}, CultureInfo.CurrentCulture);
    }
}
Imports System.Reflection
Imports System.Globalization

Public Class MyBinder
    Inherits Binder
    Public Sub New()
        MyBase.new()
    End Sub
    Private Class BinderState
        Public args() As Object
    End Class

    Public Overrides Function BindToField(ByVal bindingAttr As BindingFlags, ByVal match() As FieldInfo, ByVal value As Object, ByVal culture As CultureInfo) As FieldInfo
        If match Is Nothing Then
            Throw New ArgumentNullException("match")
        End If
        ' Get a field for which the value parameter can be converted to the specified field type.
        Dim i As Integer
        For i = 0 To match.Length - 1
            If Not (ChangeType(value, match(i).FieldType, culture) Is Nothing) Then
                Return match(i)
            End If
        Next i
        Return Nothing
    End Function 'BindToField

    Public Overrides Function BindToMethod(ByVal bindingAttr As BindingFlags, ByVal match() As MethodBase, ByRef args() As Object, ByVal modifiers() As ParameterModifier, ByVal culture As CultureInfo, ByVal names() As String, ByRef state As Object) As MethodBase
        ' Store the arguments to the method in a state object.
        Dim myBinderState As New BinderState()
        Dim arguments() As Object = New [Object](args.Length) {}
        args.CopyTo(arguments, 0)
        myBinderState.args = arguments
        state = myBinderState

        If match Is Nothing Then
            Throw New ArgumentNullException()
        End If
        ' Find a method that has the same parameters as those of args.
        Dim i As Integer
        For i = 0 To match.Length - 1
            ' Count the number of parameters that match.
            Dim count As Integer = 0
            Dim parameters As ParameterInfo() = match(i).GetParameters()
            ' Go on to the next method if the number of parameters do not match.
            If args.Length <> parameters.Length Then
                GoTo ContinueFori
            End If
            ' Match each of the parameters that the user expects the method to have.
            Dim j As Integer
            For j = 0 To args.Length - 1
                ' If names is not null, then reorder args.
                If Not (names Is Nothing) Then
                    If names.Length <> args.Length Then
                        Throw New ArgumentException("names and args must have the same number of elements.")
                    End If
                    Dim k As Integer
                    For k = 0 To names.Length - 1
                        If String.Compare(parameters(j).Name, names(k).ToString()) = 0 Then
                            args(j) = myBinderState.args(k)
                        End If
                    Next k
                End If ' Determine whether the types specified by the user can be converted to parameter type.
                If Not (ChangeType(args(j), parameters(j).ParameterType, culture) Is Nothing) Then
                    count += 1
                Else
                    Exit For
                End If
            Next j
            ' Determine whether the method has been found.
            If count = args.Length Then
                Return match(i)
            End If
ContinueFori:
        Next i
        Return Nothing
    End Function 'BindToMethod

    Public Overrides Function ChangeType(ByVal value As Object, ByVal myChangeType As Type, ByVal culture As CultureInfo) As Object
        ' Determine whether the value parameter can be converted to a value of type myType.
        If CanConvertFrom(value.GetType(), myChangeType) Then
            ' Return the converted object.
            Return Convert.ChangeType(value, myChangeType)
            ' Return null.
        Else
            Return Nothing
        End If
    End Function 'ChangeType

    Public Overrides Sub ReorderArgumentArray(ByRef args() As Object, ByVal state As Object)
        'Redimension the array to hold the state values.
        ReDim args(CType(state, BinderState).args.Length)
        ' Return the args that had been reordered by BindToMethod.
        CType(state, BinderState).args.CopyTo(args, 0)
    End Sub

    Public Overrides Function SelectMethod(ByVal bindingAttr As BindingFlags, ByVal match() As MethodBase, ByVal types() As Type, ByVal modifiers() As ParameterModifier) As MethodBase
        If match Is Nothing Then
            Throw New ArgumentNullException("match")
        End If
        Dim i As Integer
        For i = 0 To match.Length - 1
            ' Count the number of parameters that match.
            Dim count As Integer = 0
            Dim parameters As ParameterInfo() = match(i).GetParameters()
            ' Go on to the next method if the number of parameters do not match.
            If types.Length <> parameters.Length Then
                GoTo ContinueFori
            End If
            ' Match each of the parameters that the user expects the method to have.
            Dim j As Integer
            For j = 0 To types.Length - 1
                ' Determine whether the types specified by the user can be converted to parameter type.
                If CanConvertFrom(types(j), parameters(j).ParameterType) Then
                    count += 1
                Else
                    Exit For
                End If
            Next j ' Determine whether the method has been found.
            If count = types.Length Then
                Return match(i)
            End If
ContinueFori:
        Next i
        Return Nothing
    End Function 'SelectMethod
    Public Overrides Function SelectProperty(ByVal bindingAttr As BindingFlags, ByVal match() As PropertyInfo, ByVal returnType As Type, ByVal indexes() As Type, ByVal modifiers() As ParameterModifier) As PropertyInfo
        If match Is Nothing Then
            Throw New ArgumentNullException("match")
        End If
        Dim i As Integer
        For i = 0 To match.Length - 1
            ' Count the number of indexes that match.
            Dim count As Integer = 0
            Dim parameters As ParameterInfo() = match(i).GetIndexParameters()

            ' Go on to the next property if the number of indexes do not match.
            If indexes.Length <> parameters.Length Then
                GoTo ContinueFori
            End If
            ' Match each of the indexes that the user expects the property to have.
            Dim j As Integer
            For j = 0 To indexes.Length - 1
                ' Determine whether the types specified by the user can be converted to index type.
                If CanConvertFrom(indexes(j), parameters(j).ParameterType) Then
                    count += 1
                Else
                    Exit For
                End If
            Next j ' Determine whether the property has been found.
            If count = indexes.Length Then
                ' Determine whether the return type can be converted to the properties type.
                If CanConvertFrom(returnType, match(i).PropertyType) Then
                    Return match(i)
                Else
                    GoTo ContinueFori
                End If
            End If
ContinueFori:
        Next i
        Return Nothing
    End Function 'SelectProperty

    ' Determine whether type1 can be converted to type2. Check only for primitive types.
    Private Function CanConvertFrom(ByVal type1 As Type, ByVal type2 As Type) As Boolean
        If type1.IsPrimitive And type2.IsPrimitive Then
            Dim typeCode1 As TypeCode = Type.GetTypeCode(type1)
            Dim typeCode2 As TypeCode = Type.GetTypeCode(type2)
            ' If both type1 and type2 have same type, return true.
            If typeCode1 = typeCode2 Then
                Return True
            End If ' Possible conversions from Char follow.
            If typeCode1 = TypeCode.Char Then
                Select Case typeCode2
                    Case TypeCode.UInt16
                        Return True
                    Case TypeCode.UInt32
                        Return True
                    Case TypeCode.Int32
                        Return True
                    Case TypeCode.UInt64
                        Return True
                    Case TypeCode.Int64
                        Return True
                    Case TypeCode.Single
                        Return True
                    Case TypeCode.Double
                        Return True
                    Case Else
                        Return False
                End Select
            End If ' Possible conversions from Byte follow.
            If typeCode1 = TypeCode.Byte Then
                Select Case typeCode2
                    Case TypeCode.Char
                        Return True
                    Case TypeCode.UInt16
                        Return True
                    Case TypeCode.Int16
                        Return True
                    Case TypeCode.UInt32
                        Return True
                    Case TypeCode.Int32
                        Return True
                    Case TypeCode.UInt64
                        Return True
                    Case TypeCode.Int64
                        Return True
                    Case TypeCode.Single
                        Return True
                    Case TypeCode.Double
                        Return True
                    Case Else
                        Return False
                End Select
            End If ' Possible conversions from SByte follow.
            If typeCode1 = TypeCode.SByte Then
                Select Case typeCode2
                    Case TypeCode.Int16
                        Return True
                    Case TypeCode.Int32
                        Return True
                    Case TypeCode.Int64
                        Return True
                    Case TypeCode.Single
                        Return True
                    Case TypeCode.Double
                        Return True
                    Case Else
                        Return False
                End Select
            End If ' Possible conversions from UInt16 follow.
            If typeCode1 = TypeCode.UInt16 Then
                Select Case typeCode2
                    Case TypeCode.UInt32
                        Return True
                    Case TypeCode.Int32
                        Return True
                    Case TypeCode.UInt64
                        Return True
                    Case TypeCode.Int64
                        Return True
                    Case TypeCode.Single
                        Return True
                    Case TypeCode.Double
                        Return True
                    Case Else
                        Return False
                End Select
            End If ' Possible conversions from Int16 follow.
            If typeCode1 = TypeCode.Int16 Then
                Select Case typeCode2
                    Case TypeCode.Int32
                        Return True
                    Case TypeCode.Int64
                        Return True
                    Case TypeCode.Single
                        Return True
                    Case TypeCode.Double
                        Return True
                    Case Else
                        Return False
                End Select
            End If ' Possible conversions from UInt32 follow.
            If typeCode1 = TypeCode.UInt32 Then
                Select Case typeCode2
                    Case TypeCode.UInt64
                        Return True
                    Case TypeCode.Int64
                        Return True
                    Case TypeCode.Single
                        Return True
                    Case TypeCode.Double
                        Return True
                    Case Else
                        Return False
                End Select
            End If ' Possible conversions from Int32 follow.
            If typeCode1 = TypeCode.Int32 Then
                Select Case typeCode2
                    Case TypeCode.Int64
                        Return True
                    Case TypeCode.Single
                        Return True
                    Case TypeCode.Double
                        Return True
                    Case Else
                        Return False
                End Select
            End If ' Possible conversions from UInt64 follow.
            If typeCode1 = TypeCode.UInt64 Then
                Select Case typeCode2
                    Case TypeCode.Single
                        Return True
                    Case TypeCode.Double
                        Return True
                    Case Else
                        Return False
                End Select
            End If ' Possible conversions from Int64 follow.
            If typeCode1 = TypeCode.Int64 Then
                Select Case typeCode2
                    Case TypeCode.Single
                        Return True
                    Case TypeCode.Double
                        Return True
                    Case Else
                        Return False
                End Select
            End If ' Possible conversions from Single follow.
            If typeCode1 = TypeCode.Single Then
                Select Case typeCode2
                    Case TypeCode.Double
                        Return True
                    Case Else
                        Return False
                End Select
            End If
        End If
        Return False
    End Function 'CanConvertFrom
End Class


Public Class MyClass1
    Public myFieldB As Short
    Public myFieldA As Integer

    Public Overloads Sub MyMethod(ByVal i As Long, ByVal k As Char)
        Console.WriteLine(ControlChars.NewLine & "This is MyMethod(long i, char k).")
    End Sub

    Public Overloads Sub MyMethod(ByVal i As Long, ByVal j As Long)
        Console.WriteLine(ControlChars.NewLine & "This is MyMethod(long i, long j).")
    End Sub
End Class


Public Class Binder_Example
    Public Shared Sub Main()
        ' Get the type of MyClass1.
        Dim myType As Type = GetType(MyClass1)
        ' Get the instance of MyClass1.
        Dim myInstance As New MyClass1()
        Console.WriteLine(ControlChars.Cr & "Displaying the results of using the MyBinder binder.")
        Console.WriteLine()
        ' Get the method information for MyMethod.
        Dim myMethod As MethodInfo = myType.GetMethod("MyMethod", BindingFlags.Public Or BindingFlags.Instance, New MyBinder(), New Type() {GetType(Short), GetType(Short)}, Nothing)
        Console.WriteLine(MyMethod)
        ' Invoke MyMethod.
        myMethod.Invoke(myInstance, BindingFlags.InvokeMethod, New MyBinder(), New [Object]() {CInt(32), CInt(32)}, CultureInfo.CurrentCulture)
    End Sub
End Class

Comentarios

Este método invoca dinámicamente el método reflejado por esta instancia en objy pasa los parámetros especificados. Si el método es estático, se omite el obj parámetro . Para los métodos no estáticos, obj debe ser una instancia de una clase que hereda o declara el método y debe ser el mismo tipo que esta clase. Si el método no tiene parámetros, el valor de parameters debe ser null. De lo contrario, el número, el tipo y el orden de los elementos de parameters deben ser idénticos al número, el tipo y el orden de los parámetros para el método reflejado por esta instancia.

Es posible que no omita parámetros opcionales en las llamadas a Invoke. Para invocar un método y omitir parámetros opcionales, llame a Type.InvokeMember en su lugar.

Nota

Si se usa esta sobrecarga de método para invocar un constructor de instancia, se reinicializa el objeto proporcionado para obj ; es decir, se ejecutan todos los inicializadores de instancia. El valor devuelto es null. Si se invoca un constructor de clase, la clase se reinicializa; es decir, se ejecutan todos los inicializadores de clase. El valor devuelto es null.

En el caso de los parámetros primitivos pass-by-value, se realiza el ampliación normal (por ejemplo, Int16 -> Int32). En el caso de los parámetros de referencia de paso a valor, se permite el ampliación de referencia normal (clase derivada a clase base y clase base al tipo de interfaz). Sin embargo, para los parámetros primitivos de paso a referencia, los tipos deben coincidir exactamente. En el caso de los parámetros de referencia de paso a referencia, se sigue aplicando la ampliación normal.

Por ejemplo, si el método reflejado por esta instancia se declara como public boolean Compare(String a, String b), parameters debe ser una matriz de Objects con longitud 2 de modo que parameters[0] = new Object("SomeString1") and parameters[1] = new Object("SomeString2").

Si un parámetro del método actual es un tipo de valor y el argumento correspondiente de parameters es null, el tiempo de ejecución pasa una instancia inicializada de cero del tipo de valor.

La reflexión usa la búsqueda de métodos dinámicos al invocar métodos virtuales. Por ejemplo, suponga que la clase B hereda de la clase A e implementa un método virtual denominado M. Ahora supongamos que tiene un MethodInfo objeto que representa M en la clase A. Si usa el Invoke método para invocar M en un objeto de tipo B, la reflexión usará la implementación dada por la clase B. Incluso si el objeto de tipo B se convierte en A, se usa la implementación dada por la clase B (vea el ejemplo de código siguiente).

Por otro lado, si el método no es virtual, la reflexión usará la implementación dada por el tipo del que MethodInfo se obtuvo, independientemente del tipo del objeto pasado como destino.

Las restricciones de acceso se omiten para el código de plena confianza. Es decir, se puede acceder a constructores privados, métodos, campos y propiedades e invocarse a través de la reflexión siempre que el código sea de plena confianza.

Si el método invocado produce una excepción, el Exception.GetBaseException método devuelve la excepción de origen.

Nota

A partir de .NET Framework 2.0, este método se puede usar para acceder a miembros no públicos si se ha concedido ReflectionPermission al autor de la llamada con la ReflectionPermissionFlag.RestrictedMemberAccess marca y si el conjunto de concesión de los miembros no públicos está restringido al conjunto de concesión del autor de la llamada o a un subconjunto de ellos. (Consulte Consideraciones de seguridad para la reflexión). Para usar esta funcionalidad, la aplicación debe tener como destino .NET Framework 3.5 o posterior.

Consulte también

Se aplica a