Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Reflection;
  3.  
  4. namespace Recetas.Cap03
  5. {
  6.     public sealed class ReflectionConInvokeMethod
  7.     {
  8.         public static void ImprimirMensaje(string param1, int param2, char param3)
  9.         {
  10.             Console.WriteLine ("\nValores de `param1`: {0}; `param2`: {1}; `param3`: {2}\n",
  11.                                param1, param2.ToString(), param3.ToString());
  12.         }
  13.        
  14.         public static void Main()
  15.         {
  16.             // Creación objeto de `ReflectionConInvokeMethod`:
  17.             object objeto = new ReflectionConInvokeMethod();
  18.            
  19.             // Obtención de representación `Type` de `ReflectionConInvokeMethod`:
  20.             Type tipo = typeof(ReflectionConInvokeMethod);
  21.            
  22.             // Obtiene representación `MethodInfo` con el nombre
  23.             // y parámetros especificados en los argumentos de `GetMethod`:
  24.             MethodInfo infoMetodo = tipo.GetMethod ("ImprimirMensaje",
  25.                 new Type[] { typeof(string), typeof(int), typeof(char) }
  26.             );
  27.            
  28.             // Invoca el método con los argumentos espeificados en `InvokeMember`:
  29.             tipo.InvokeMember ("ImprimirMensaje", BindingFlags.InvokeMethod,
  30.                 null, objeto, new Object[] { "Blog xCSw", 235, '#'}
  31.             );
  32.            
  33.             // Uso de `Invoke` de `MethodInfo` para invocar el
  34.             // método `ImprimirMensaje`:
  35.             infoMetodo.Invoke( null, BindingFlags.InvokeMethod,
  36.                 null, new Object[] { "Blog xCSw", 235, '#'}, null
  37.             );
  38.         }
  39.     }
  40. }