)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Enumeration ()

Public general use code classes and xml files that we've compiled and used over the years:

Enumeration class. Extends enumeration to class like behaviour.

    1: using System;
    2: using System.Collections.Generic;
    3: using System.Linq;
    4: using System.Reflection;
    5:  
    6: ////////////////////////////////////////////////////////////////////////////
    7:  
    8: /// <summary publish="true">
    9: /// Enumeration class. Extends enumeration to class like behaviour.
   10: /// </summary>
   11: /// <remarks> 
   12: /// Copyright © 2021-2022 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   13: ///
   14: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   15: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   16: ///
   17: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   19: /// 
   20: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   21: /// 
   22: /// Copyright notice: This notice may not be removed or altered from any source distribution.
   23: /// </remarks> 
   24:  
   25: ////////////////////////////////////////////////////////////////////////////
   26:  
   27: /// <summary>
   28: ///
   29: /// </summary>
   30: public abstract class Enumeration : IComparable
   31: {
   32:     // https://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/
   33:  
   34:     private int _id;
   35:     private string _name;
   36:     private string _assembly;
   37:  
   38:     ////////////////////////////////////////////////////////////////////////////
   39:  
   40:     /// <summary>
   41:     ///
   42:     /// </summary>
   43:     protected Enumeration()
   44:     {
   45:     }
   46:  
   47:     /// <summary/>
   48:     protected Enumeration(string name)
   49:     {
   50:         _id = 0;
   51:         _name = name;
   52:         _assembly = string.Empty;
   53:     }
   54:  
   55:     /// <summary/>
   56:     protected Enumeration(int id, string name)
   57:     {
   58:         _id = id;
   59:         _name = name;
   60:         _assembly = string.Empty;
   61:     }
   62:  
   63:     /// <summary/>
   64:     protected Enumeration(string name, string assembly)
   65:     {
   66:         _name = name;
   67:         _assembly = assembly;
   68:     }
   69:  
   70:     /// <summary/>
   71:     protected Enumeration(int id, string name, string assembly)
   72:     {
   73:         _id = id;
   74:         _name = name;
   75:         _assembly = assembly;
   76:     }
   77:  
   78:     /// <summary/>
   79:     public int Id
   80:     {
   81:         get { return _id; }
   82:         set { _id = value; }
   83:     }
   84:  
   85:     /// <summary/>
   86:     public string Name
   87:     {
   88:         get { return _name; }
   89:         set { _name = value; }
   90:     }
   91:  
   92:     /// <summary/>
   93:     public string Assembly
   94:     {
   95:         get { return _assembly; }
   96:         set { _assembly = value; }
   97:     }
   98:  
   99:     /// <summary/>
  100:     public override string ToString()
  101:     {
  102:         return Name;
  103:     }
  104:  
  105:     ////////////////////////////////////////////////////////////////////////////
  106:  
  107:     /// <summary>
  108:     ///
  109:     /// </summary>
  110:     public static IEnumerable<T> GetAll<T>() where T : Enumeration, new()
  111:     {
  112:         var type = typeof(T);
  113:         var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
  114:  
  115:         foreach (var info in fields)
  116:         {
  117:             var instance = new T();
  118:             var locatedValue = info.GetValue(instance) as T;
  119:  
  120:             if (locatedValue != null)
  121:             {
  122:                 yield return locatedValue;
  123:             }
  124:         }
  125:     }
  126:  
  127:     ////////////////////////////////////////////////////////////////////////////
  128:  
  129:     /// <summary>
  130:     ///
  131:     /// </summary>
  132:     public override bool Equals(object obj)
  133:     {
  134:         var otherValue = obj as Enumeration;
  135:  
  136:         if (otherValue == null)
  137:         {
  138:             return false;
  139:         }
  140:  
  141:         var typeMatches = GetType().Equals(obj.GetType());
  142:         var valueMatches = _id.Equals(otherValue.Id);
  143:  
  144:         return typeMatches && valueMatches;
  145:     }
  146:  
  147:     ////////////////////////////////////////////////////////////////////////////
  148:  
  149:     /// <summary>
  150:     ///
  151:     /// </summary>
  152:     public override int GetHashCode()
  153:     {
  154:         return _id.GetHashCode();
  155:     }
  156:  
  157:     ////////////////////////////////////////////////////////////////////////////
  158:  
  159:     /// <summary>
  160:     ///
  161:     /// </summary>
  162:     public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue)
  163:     {
  164:         var absoluteDifference = Math.Abs(firstValue.Id - secondValue.Id);
  165:         return absoluteDifference;
  166:     }
  167:  
  168:     ////////////////////////////////////////////////////////////////////////////
  169:  
  170:     /// <summary>
  171:     ///
  172:     /// </summary>
  173:     public static T FromValue<T>(int id) where T : Enumeration, new()
  174:     {
  175:         var matchingItem = parse<T, int>(id, "id", item => item.Id == id);
  176:         return matchingItem;
  177:     }
  178:  
  179:     ////////////////////////////////////////////////////////////////////////////
  180:  
  181:     /// <summary>
  182:     ///
  183:     /// </summary>
  184:     public static T FromName<T>(string name) where T : Enumeration, new()
  185:     {
  186:         var matchingItem = parse<T, string>(name, "name", item => item.Name == name);
  187:         return matchingItem;
  188:     }
  189:  
  190:     ////////////////////////////////////////////////////////////////////////////
  191:  
  192:     /// <summary>
  193:     ///
  194:     /// </summary>
  195:     private static T parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration, new()
  196:     {
  197:         var matchingItem = GetAll<T>().FirstOrDefault(predicate);
  198:  
  199:         if (matchingItem == null)
  200:         {
  201:             var message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(T));
  202:             throw new ApplicationException(message);
  203:         }
  204:  
  205:         return matchingItem;
  206:     }
  207:  
  208:     ////////////////////////////////////////////////////////////////////////////
  209:  
  210:     /// <summary>
  211:     ///
  212:     /// </summary>
  213:     public int CompareTo(object other)
  214:     {
  215:         return Id.CompareTo(((Enumeration)other).Id);
  216:     }
  217:  
  218:     ////////////////////////////////////////////////////////////////////////////
  219:     ////////////////////////////////////////////////////////////////////////////
  220: }
  221: