Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Last active August 29, 2015 14:02
Show Gist options
  • Save Fhernd/dc0fe531f3372df228c4 to your computer and use it in GitHub Desktop.
Save Fhernd/dc0fe531f3372df228c4 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)
{
UsoTypeGetConstructor obj = (UsoTypeGetConstructor) constructor.Invoke( new object[] {13});
obj.MostrarMensaje();
}
else
{
Console.WriteLine("No se encontré ningún constructor con la firma especificada.\n");
}
}
catch (Exception e)
{
// Area de tratamiento de la excepción.
}
}
public void MostrarMensaje()
{
Console.WriteLine ("Blog xCSw");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment