Created
June 7, 2014 20:32
-
-
Save Fhernd/d5ed027d59f3acb6cdb1 to your computer and use it in GitHub Desktop.
Demostración del uso de los métodos Invoke (MethodInfo) y InvokeMember (Type) en C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
namespace Recetas.Cap03 | |
{ | |
public sealed class ReflectionConInvokeMethod | |
{ | |
public static void ImprimirMensaje(string param1, int param2, char param3) | |
{ | |
Console.WriteLine ("\nValores de `param1`: {0}; `param2`: {1}; `param3`: {2}\n", | |
param1, param2.ToString(), param3.ToString()); | |
} | |
public static void Main() | |
{ | |
// Creación objeto de `ReflectionConInvokeMethod`: | |
object objeto = new ReflectionConInvokeMethod(); | |
// Obtención de representación `Type` de `ReflectionConInvokeMethod`: | |
Type tipo = typeof(ReflectionConInvokeMethod); | |
// Obtiene representación `MethodInfo` con el nombre | |
// y parámetros especificados en los argumentos de `GetMethod`: | |
MethodInfo infoMetodo = tipo.GetMethod ("ImprimirMensaje", | |
new Type[] { typeof(string), typeof(int), typeof(char) } | |
); | |
// Invoca el método con los argumentos espeificados en `InvokeMember`: | |
tipo.InvokeMember ("ImprimirMensaje", BindingFlags.InvokeMethod, | |
null, objeto, new Object[] { "Blog xCSw", 235, '#'} | |
); | |
// Uso de `Invoke` de `MethodInfo` para invocar el | |
// método `ImprimirMensaje`: | |
infoMetodo.Invoke( null, BindingFlags.InvokeMethod, | |
null, new Object[] { "Blog xCSw", 235, '#'}, null | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment