Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created June 7, 2014 17:39
Show Gist options
  • Save Fhernd/2cf068f23db0abd96efa to your computer and use it in GitHub Desktop.
Save Fhernd/2cf068f23db0abd96efa to your computer and use it in GitHub Desktop.
Demostración del descubrimiento de miembros de un tipo con Reflection en C#.
// ===++===
//
// OrtizOL
//
// ===--===
/*============================================================
//
// Clase: ObtencionMiembrosConReflection.cs
//
// Propósito: Demostración del descubrimiento de miembros
// de un tipo con Reflection.
//
============================================================*/
using System;
using System.Reflection;
namespace Recetas.Cap03
{
// Clase de ejemplo para uso de reflection:
public sealed class ObtencionMiembrosConReflection
{
public static void Main()
{
// Obtención representación `Type` de `ObtencionMiembrosConReflection`:
Type tipo = typeof(ObtencionMiembrosConReflection);
// Descubrimios los constructores public:
Console.WriteLine ("\n === Constructores de `{0}` === \n", typeof(ObtencionMiembrosConReflection).Name.ToString());
foreach (ConstructorInfo ctor in tipo.GetConstructors())
{
Console.WriteLine ("\tConstructor: {0}", ctor.ToString());
// Parámetros para el constructor actual en `ctor`:
foreach (ParameterInfo parametro in ctor.GetParameters())
{
Console.WriteLine ("\t{0} ({1})", parametro.Name.ToString(), parametro.ParameterType.ToString());
}
Console.WriteLine ();
}
// Descubrimios los métodos public:
Console.WriteLine ("\n === Métodos de `{0}` === \n", typeof(ObtencionMiembrosConReflection).Name.ToString());
foreach (MethodInfo metodo in tipo.GetMethods())
{
Console.WriteLine ("\tMétodo: {0}", metodo.ToString());
// Parámetros para el construmetodo actual en `metodo`:
foreach (ParameterInfo parametro in metodo.GetParameters())
{
Console.WriteLine ("\t{0} ({1})", parametro.Name.ToString(), parametro.ParameterType.ToString());
}
Console.WriteLine ();
}
// Descubrimos las propiedades:
Console.WriteLine ("\n === Propiedades de `{0}` === \n", typeof(ObtencionMiembrosConReflection).Name.ToString());
foreach (PropertyInfo propiedad in tipo.GetProperties())
{
Console.WriteLine ("\tPropiedad: {0}", propiedad.ToString());
// Muestra las funciones de acceso de esta propiedad:
foreach (MethodInfo funcionAcceso in propiedad.GetAccessors())
{
Console.WriteLine ("\t{0}", funcionAcceso.Name.ToString());
}
Console.WriteLine ();
}
}
public string Propiedad
{
get;
set;
}
public ObtencionMiembrosConReflection(string param1, string param2, int param3)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment