fcamuso

Metodo Helper con reflection per enum

Feb 7th, 2021
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Reflection;
  5.  
  6. namespace Enum2
  7. {
  8.   class Program
  9.   {
  10.     static void Main(string[] args)
  11.     {
  12.       DocumentoTipo tipo = DocumentoTipo.NotaDiCredito;
  13.       Console.WriteLine($"Val: {tipo}");
  14.       //Console.WriteLine($"Val: {tipo.GetString()}"); //extension method
  15.       Console.WriteLine($"Val: {MyHelpers.GetEnumDescription(tipo)}");
  16.  
  17.       Prova p = new Prova();
  18.       //MyExtensions.F(p, 8);
  19.  
  20.       string s = "topolino";
  21.       //Console.WriteLine(s.xxx());
  22.  
  23.       Allarme allarme = Allarme.InteraCasa;
  24.       Console.WriteLine(MyHelpers.GetEnumDescription(allarme));
  25.  
  26.       Console.ReadKey();
  27.     }
  28.   }
  29.  
  30.   public enum Allarme : byte
  31.   {
  32.     [Description("Tutti gli allarmi spenti")]
  33.     Spento = 0,
  34.  
  35.     [Description("Solo Giardino")]
  36.     Giardino = 1,
  37.  
  38.     [Description("Solo Garage")]
  39.     Garage = 2,
  40.  
  41.     [Description("Allarmi tutti accesi")]
  42.     InteraCasa = 3
  43.   };
  44.  
  45.   public enum DocumentoTipo
  46.   {
  47.     //[Description("Fattura")] // non necessario se la costante coincide con ciò che vogliamo visualizzare
  48.     Fattura,
  49.  
  50.     [Description("Nota Di Credito")]
  51.     [DefaultValue(10)]
  52.     NotaDiCredito
  53.   }
  54.  
  55.   public class Prova
  56.   {
  57.     public void F(object obj) { Console.WriteLine("metodo classe"); }
  58.     string s = "privata";
  59.   }
  60.  
  61.  
  62.   static public class MyExtensions
  63.   {
  64.     static private readonly Dictionary<DocumentoTipo, string> DocStrings = new Dictionary<DocumentoTipo, string> {
  65.                 { DocumentoTipo.Fattura, "Fatture" },
  66.                 { DocumentoTipo.NotaDiCredito, "Nota Di Credito" }
  67.             };
  68.  
  69.  
  70.     //public static double xxx(this String s) { return 5.89; }
  71.  
  72.     ////public static void F(this Prova p, int n) { Console.WriteLine(p.s); }
  73.  
  74.     public static string GetString(this DocumentoTipo e)
  75.     {
  76.  
  77.       return (MyExtensions.DocStrings )[e];
  78.            
  79.  
  80.       //  switch ((int)e)
  81.       //  {
  82.       //    case 0: return "Fattura";
  83.       //    case 1: return "Nota Di Credito";
  84.       //    default: return "Tipo Documento non riconosciuto";
  85.       //  }
  86.  
  87.       //return strings[e];
  88.     }
  89.   }
  90.  
  91.       static public class MyHelpers
  92.   {
  93.     // Il metodo verifica la presenza dell'attributo Description,
  94.     //altrimenti necessario attributo nell'enumeratore
  95.     static public string GetEnumDescription(object enumObj)
  96.     {
  97.       FieldInfo fi = enumObj.GetType().GetField(enumObj.ToString());
  98.  
  99.       DescriptionAttribute[] attributes =
  100.           (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
  101.      
  102.       return (attributes.Length > 0) ? attributes[0].Description : enumObj.ToString();
  103.     }
  104.   }
  105. }
  106.  
  107.  
Add Comment
Please, Sign In to add comment