Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a solution. Consider the following:</p> <pre><code>public sealed class AccountStatus { public static readonly AccountStatus Open = new AccountStatus(1); public static readonly AccountStatus Closed = new AccountStatus(2); public static readonly SortedList&lt;byte, AccountStatus&gt; Values = new SortedList&lt;byte, AccountStatus&gt;(); private readonly byte Value; private AccountStatus(byte value) { this.Value = value; Values.Add(value, this); } public static implicit operator AccountStatus(byte value) { return Values[byte]; } public static implicit operator byte(AccountStatus value) { return value.Value; } } </code></pre> <p>The above offers implicit conversion:</p> <pre><code> AccountStatus openedAccount = 1; // Works byte openedValue = AccountStatus.Open; // Works </code></pre> <p>This is a fair bit more work than declaring a normal enum (though you can refactor some of the above into a common generic base class). You can go even further by having the base class implement IComparable &amp; IEquatable, as well as adding methods to return the value of DescriptionAttributes, declared names, etc, etc.</p> <p>I wrote a base class (RichEnum&lt;>) to handle most fo the grunt work, which eases the above declaration of enums down to:</p> <pre><code>public sealed class AccountStatus : RichEnum&lt;byte, AccountStatus&gt; { public static readonly AccountStatus Open = new AccountStatus(1); public static readonly AccountStatus Closed = new AccountStatus(2); private AccountStatus(byte value) : base (value) { } public static implicit operator AccountStatus(byte value) { return Convert(value); } } </code></pre> <p>The base class (RichEnum) is listed below.</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Resources; namespace Ethica { using Reflection; using Text; [DebuggerDisplay("{Value} ({Name})")] public abstract class RichEnum&lt;TValue, TDerived&gt; : IEquatable&lt;TDerived&gt;, IComparable&lt;TDerived&gt;, IComparable, IComparer&lt;TDerived&gt; where TValue : struct , IComparable&lt;TValue&gt;, IEquatable&lt;TValue&gt; where TDerived : RichEnum&lt;TValue, TDerived&gt; { #region Backing Fields /// &lt;summary&gt; /// The value of the enum item /// &lt;/summary&gt; public readonly TValue Value; /// &lt;summary&gt; /// The public field name, determined from reflection /// &lt;/summary&gt; private string _name; /// &lt;summary&gt; /// The DescriptionAttribute, if any, linked to the declaring field /// &lt;/summary&gt; private DescriptionAttribute _descriptionAttribute; /// &lt;summary&gt; /// Reverse lookup to convert values back to local instances /// &lt;/summary&gt; private static SortedList&lt;TValue, TDerived&gt; _values; private static bool _isInitialized; #endregion #region Constructors protected RichEnum(TValue value) { if (_values == null) _values = new SortedList&lt;TValue, TDerived&gt;(); this.Value = value; _values.Add(value, (TDerived)this); } #endregion #region Properties public string Name { get { CheckInitialized(); return _name; } } public string Description { get { CheckInitialized(); if (_descriptionAttribute != null) return _descriptionAttribute.Description; return _name; } } #endregion #region Initialization private static void CheckInitialized() { if (!_isInitialized) { ResourceManager _resources = new ResourceManager(typeof(TDerived).Name, typeof(TDerived).Assembly); var fields = typeof(TDerived) .GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public) .Where(t =&gt; t.FieldType == typeof(TDerived)); foreach (var field in fields) { TDerived instance = (TDerived)field.GetValue(null); instance._name = field.Name; instance._descriptionAttribute = field.GetAttribute&lt;DescriptionAttribute&gt;(); var displayName = field.Name.ToPhrase(); } _isInitialized = true; } } #endregion #region Conversion and Equality public static TDerived Convert(TValue value) { return _values[value]; } public static bool TryConvert(TValue value, out TDerived result) { return _values.TryGetValue(value, out result); } public static implicit operator TValue(RichEnum&lt;TValue, TDerived&gt; value) { return value.Value; } public static implicit operator RichEnum&lt;TValue, TDerived&gt;(TValue value) { return _values[value]; } public static implicit operator TDerived(RichEnum&lt;TValue, TDerived&gt; value) { return value; } public override string ToString() { return _name; } #endregion #region IEquatable&lt;TDerived&gt; Members public override bool Equals(object obj) { if (obj != null) { if (obj is TValue) return Value.Equals((TValue)obj); if (obj is TDerived) return Value.Equals(((TDerived)obj).Value); } return false; } bool IEquatable&lt;TDerived&gt;.Equals(TDerived other) { return Value.Equals(other.Value); } public override int GetHashCode() { return Value.GetHashCode(); } #endregion #region IComparable Members int IComparable&lt;TDerived&gt;.CompareTo(TDerived other) { return Value.CompareTo(other.Value); } int IComparable.CompareTo(object obj) { if (obj != null) { if (obj is TValue) return Value.CompareTo((TValue)obj); if (obj is TDerived) return Value.CompareTo(((TDerived)obj).Value); } return -1; } int IComparer&lt;TDerived&gt;.Compare(TDerived x, TDerived y) { return (x == null) ? -1 : (y == null) ? 1 : x.Value.CompareTo(y.Value); } #endregion public static IEnumerable&lt;TDerived&gt; Values { get { return _values.Values; } } public static TDerived Parse(string name) { foreach (TDerived value in _values.Values) if (0 == string.Compare(value.Name, name, true) || 0 == string.Compare(value.DisplayName, name, true)) return value; return null; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload