SHOW:
|
|
- or go back to the newest paste.
| 1 | using System; | |
| 2 | using System.Collections.Generic; | |
| 3 | using System.Text; | |
| 4 | ||
| 5 | // OJO para usar "dynamic" hay que tener una referencia a Microsof.CSharp | |
| 6 | ||
| 7 | // http://www.elguillemola.com/index.php/2013/01/si-utilizas-dynamic-en-csharp-acuerdate-de-agregar-una-referencia-a-microsoft-csharp/ | |
| 8 | ||
| 9 | ||
| 10 | // si quieres saber cómo hacer esto mismo en VB: | |
| 11 | // http://pastebin.com/NEAas23n | |
| 12 | ||
| 13 | ||
| 14 | namespace ActivatorCS | |
| 15 | {
| |
| 16 | public sealed class CrearClase2 | |
| 17 | {
| |
| 18 | public static void prueba() | |
| 19 | {
| |
| 20 | ||
| 21 | // La clase a crear | |
| 22 | var elNombre = "ActivatorCS.Colega"; | |
| 23 | ||
| 24 | crearClase(elNombre); | |
| 25 | ||
| 26 | // Esta clase se llama igual, | |
| 27 | // pero no es del mismo tipo | |
| 28 | // y no pasaría la prueba de IsAssignableFrom, | |
| 29 | // aunque no daría error al hacer la conversión | |
| 30 | // pero no crearía una instancia, | |
| 31 | // por tanto Activator devolvería nulo, | |
| 32 | // en realidad el valor nulo lo devuelve | |
| 33 | // la operación de conversión de tipos | |
| 34 | // por eso en VB utilizo TryCast en lugar | |
| 35 | // de DirectCast o CType | |
| 36 | elNombre = "OtroEspacio.Colega"; | |
| 37 | crearClase(elNombre); | |
| 38 | ||
| 39 | // esta clase se basa en la que queremos crear | |
| 40 | // aunque esté en otro espacio de nombres | |
| 41 | elNombre = "OtroEspacio.Coleguilla"; | |
| 42 | crearClase(elNombre); | |
| 43 | ||
| 44 | //' Esta no tiene constructor | |
| 45 | //elNombre = "OtroEspacio.Coleguilla2" | |
| 46 | //crearColega(elNombre) | |
| 47 | ||
| 48 | ||
| 49 | // este tipo no existe, | |
| 50 | // pero debemos usar IsAssignableFrom | |
| 51 | // para no provocar una excepción | |
| 52 | elNombre = "Activator.Colega"; | |
| 53 | crearClase(elNombre); | |
| 54 | ||
| 55 | // esta se basa en la nuestra | |
| 56 | // pero es abstracta (no tiene constructor) | |
| 57 | // ya dará un error al crearla | |
| 58 | // aunque pasaría la prueba de IsAssignableFrom | |
| 59 | elNombre = "ActivatorCS.ColegaAbstracto"; | |
| 60 | crearClase(elNombre); | |
| 61 | ||
| 62 | ||
| 63 | } | |
| 64 | ||
| 65 | private static void crearClase(string laClase) | |
| 66 | {
| |
| 67 | var todoBien = true; | |
| 68 | dynamic obj = null; | |
| 69 | ||
| 70 | Type elTipo = Type.GetType(laClase); | |
| 71 | try | |
| 72 | {
| |
| 73 | obj = Activator.CreateInstance(elTipo); | |
| 74 | ||
| 75 | if (obj != null) | |
| 76 | {
| |
| 77 | obj.Nombre = "Pepe"; | |
| 78 | obj.Email = "[email protected]"; | |
| 79 | ||
| 80 | Console.WriteLine("Objeto creado correctamente: {0}{1}{2}, {3}",
| |
| 81 | obj.GetType().Name, "\r\n", | |
| 82 | obj.Nombre, obj.Email); | |
| 83 | } | |
| 84 | ||
| 85 | } | |
| 86 | catch (Exception ex) | |
| 87 | {
| |
| 88 | Console.WriteLine("ERROR: " + ex.Message);
| |
| 89 | todoBien = false; | |
| 90 | } | |
| 91 | ||
| 92 | if (!todoBien) | |
| 93 | {
| |
| 94 | Console.WriteLine("Parece que el tipo: {0} no es del tipo adecuado.",
| |
| 95 | laClase); | |
| 96 | } | |
| 97 | ||
| 98 | Console.WriteLine(); | |
| 99 | } | |
| 100 | ||
| 101 | } | |
| 102 | } |