Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created June 6, 2014 21:48
Show Gist options
  • Save Fhernd/721b104fd76ce17123bd to your computer and use it in GitHub Desktop.
Save Fhernd/721b104fd76ce17123bd to your computer and use it in GitHub Desktop.
Uso del método GetConstructor de la clase Type (System) en C#.
using System;
using System.Reflection;
namespace Recetas.Cap03
{
public class UsoTypeGetConstructor
{
public UsoTypeGetConstructor() { }
public UsoTypeGetConstructor(int i) { }
public static void Main()
{
try
{
Type tipo = typeof (UsoTypeGetConstructor);
// Aquí obtiene el constructor con parámetro entero (int):
ConstructorInfo constructor = tipo.GetConstructor(new Type[] {typeof(int)});
if (constructor != null)
{
Console.WriteLine ("Constructor con firma que contiene un parámetro `int`: {0}",
constructor.ToString());
}
else
{
Console.WriteLine("No se encontré ningún constructor con la firma especificada.\n");
}
}
catch (Exception e)
{
// Area de tratamiento de la excepción.
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment