Created
June 7, 2014 03:53
-
-
Save Fhernd/3be74cb158ddb932716c to your computer and use it in GitHub Desktop.
Demostración de uso de atributos personalizados y reflection 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; | |
using System.Collections; | |
namespace Recetas.Cap03 | |
{ | |
// Atributo para especificar que un tipo o | |
// miembro ha sido verificado/probado: | |
public class VerificadoAttribute : Attribute | |
{ | |
public override string ToString() | |
{ | |
return "Probado"; | |
} | |
} | |
// Atributo aplicable a estructuras y clases. | |
// Posee un argumento requerido: nombre del autor. | |
// Posee un argumento opcional: versión de la clase o estructuras | |
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] | |
public class AutorAttribute : Attribute | |
{ | |
private string nombre; | |
private int version; | |
// Atributo opcional (solo-lectura): | |
public string Nombre | |
{ | |
get | |
{ | |
return nombre; | |
} | |
} | |
// Atributo requerido (lectura-escritura): | |
public int Version | |
{ | |
get | |
{ | |
return version; | |
} | |
set | |
{ | |
version = value; | |
} | |
} | |
// Ctor para inicializar el atributo requerido: | |
public AutorAttribute(string nombre) | |
{ | |
this.nombre = nombre; | |
version = 0; | |
} | |
// Sobrescritura de `ToString`: | |
public override string ToString() | |
{ | |
string valor = "Autor: " + nombre; | |
if (version != 0) | |
{ | |
valor += " Versión: " + version.ToString(); | |
} | |
return valor; | |
} | |
} | |
// Aplicamos el atributo `AutorAttribute`: | |
[AutorAttribute("John Ortiz Ordoñez")] | |
public class Cuenta | |
{ | |
private ArrayList ordenes; | |
// Aplicamos atributo `VerificadoAttribute`: | |
[VerificadoAttribute] | |
public void AgregarOrden (Orden orden) | |
{ | |
ordenes.Add (orden); | |
} | |
} | |
// Aplica dos atributos: `AutorAttribute`, y `VerificadoAttribute`: | |
[AutorAttribute("John Ortiz Ordoñez", Version = 3), VerificadoAttribute()] | |
public class Orden | |
{ | |
// Implementación... | |
} | |
public sealed class Aplicacion | |
{ | |
public static void Main() | |
{ | |
// Muestra los atributos de la clase `Cuenta`: | |
MostrarAtributos (typeof(Cuenta)); | |
// Muestra los miembros con atributo `VerificadoAttribute`: | |
foreach (MethodInfo infoMetodo in (typeof (Cuenta)).GetMethods()) | |
{ | |
if (MiembroVerificado (infoMetodo)) | |
{ | |
Console.WriteLine ("\tEl miembro `{0}` está verificado.", infoMetodo.Name.ToString()); | |
} | |
else | |
{ | |
Console.WriteLine ("\tEl miembro `{0}` NO está verificado.", infoMetodo.Name.ToString()); | |
} | |
} | |
Console.WriteLine (); | |
// Muestra los atributos para la clase `Orden`: | |
MostrarAtributos (typeof(Orden)); | |
// Enumera los atributos de los métodos de la clase `Orden`: | |
foreach (MethodInfo infoMetodo in (typeof (Orden)).GetMethods()) | |
{ | |
if (MiembroVerificado (infoMetodo)) | |
{ | |
Console.WriteLine ("\tEl miembro `{0}` está verificado.", infoMetodo.Name.ToString()); | |
} | |
else | |
{ | |
Console.WriteLine ("\tEl miembro `{0}` NO está verificado.", infoMetodo.Name.ToString()); | |
} | |
} | |
Console.WriteLine (); | |
} | |
private static bool MiembroVerificado(MemberInfo miembro) | |
{ | |
foreach (object atributo in miembro.GetCustomAttributes(true)) | |
{ | |
if (atributo is VerificadoAttribute) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
private static void MostrarAtributos (MemberInfo miembro) | |
{ | |
Console.WriteLine ("\nConjunto de atributos para: {0}", miembro.Name.ToString()); | |
foreach (object atributo in miembro.GetCustomAttributes(true)) | |
{ | |
Console.WriteLine ("\t{0}", atributo.ToString()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment