fcamuso

Extensions Methods con Enum

Jan 31st, 2021
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 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.    
  16.       Prova p = new Prova();
  17.       //MyExtensions.F(p, 8);
  18.  
  19.       string s = "topolino";
  20.       //Console.WriteLine(s.xxx());
  21.  
  22.       Console.ReadKey();
  23.     }
  24.   }
  25.  
  26.   public enum DocumentoTipo
  27.   {
  28.     Fattura,
  29.     NotaDiCredito
  30.   }
  31.  
  32.   public class Prova
  33.   {
  34.     public void F(object obj) { Console.WriteLine("metodo classe"); }
  35.     string s = "privata";
  36.   }
  37.  
  38.   static public class MyExtensions
  39.   {
  40.     public static string GetString(this DocumentoTipo e)
  41.     {
  42.       //  switch ((int)e)
  43.       //  {
  44.       //    case 0: return "Fattura";
  45.       //    case 1: return "Nota Di Credito";
  46.       //    default: return "Tipo Documento non riconosciuto";
  47.       //  }
  48.  
  49.       Dictionary<int, string> strings = new Dictionary<int, string> {
  50.                 { 0, "Fatture" },
  51.                 { 1, "Nota Di Credito" }
  52.             };
  53.       return strings[(int)e];
  54.     }
  55.   }
  56. }
  57.  
  58.  
Add Comment
Please, Sign In to add comment