Created
June 6, 2014 21:02
-
-
Save Fhernd/4fc682fdcd0941925398 to your computer and use it in GitHub Desktop.
Demostración básica de reflection 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 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