Last active
August 29, 2015 14:02
-
-
Save Fhernd/dc0fe531f3372df228c4 to your computer and use it in GitHub Desktop.
Uso del método GetConstructor de la clase Type (System) 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 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