Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created June 6, 2014 21:02
Show Gist options
  • Save Fhernd/4fc682fdcd0941925398 to your computer and use it in GitHub Desktop.
Save Fhernd/4fc682fdcd0941925398 to your computer and use it in GitHub Desktop.
Demostración básica de reflection en C#.
using System;
using System.Reflection;
namespace Recetas.Cap03
{
public class Calculadora
{
public virtual int Sumar(int a, int b)
{
return a + b;
}
}
public class Aplicacion
{
public static void Main()
{
Console.WriteLine("\nReflection: MethodInfo");
// Crea un objeto de `Calculadora`:
Calculadora calc = new Calculadora();
// Obtención de la información del tipo:
Type infoTipo = calc.GetType();
// Información de método:
MethodInfo infoMetodo = infoTipo.GetMethod("Sumar");
// Argumentos del método `Sumar`:
object[] args = new object[] {3, 7};
// Invocación del método:
Console.WriteLine("Nombre tipo: {0}\n\tRetorno: {1}\n", infoTipo.FullName, infoMetodo.Invoke(calc, args));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment