using Newtonsoft.Json.Linq;
public static void Main()
Console.WriteLine("Hello World");
public class EnumerationResultConverter : JsonConverter
public override bool CanConvert(Type objectType)
return (objectType == typeof(Enumeration));
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
JObject jo = JObject.Load(reader);
int code = (int)jo["value"];
dynamic enumeration = Activator.CreateInstance(objectType);
return enumeration.FromValue(code);
public override bool CanWrite
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
throw new NotImplementedException();
public class InvestmentType : Enumeration
public static readonly InvestmentType None = new InvestmentType(-1, "None");
public static readonly InvestmentType Unknown = new InvestmentType(0, "Unknown");
public static readonly InvestmentType TreasuryBill = new InvestmentType(1, "TreasuryBill");
public static readonly InvestmentType Bond = new InvestmentType(2, "Bond");
public static readonly InvestmentType Stock = new InvestmentType(3, "Stock");
public static readonly InvestmentType StockBond = new InvestmentType(4, "StockBond");
public static readonly InvestmentType Blended = new InvestmentType(5, "Blended");
public static readonly InvestmentType AnnuityFund = new InvestmentType(15, "AnnuityFund");
public static readonly InvestmentType Model = new InvestmentType(23, "Model");
public static readonly InvestmentType Category = new InvestmentType(24, "Category");
public static readonly InvestmentType AllocationRule = new InvestmentType(29, "AllocationRule");
public static readonly InvestmentType Other = new InvestmentType(2147483647, "Other");
public static InvestmentType FromEdwValue(string edwValue)
if (string.IsNullOrWhiteSpace(edwValue))
return InvestmentType.Unknown;
return InvestmentType.TreasuryBill;
return InvestmentType.Bond;
return InvestmentType.Stock;
return InvestmentType.StockBond;
return InvestmentType.Blended;
return InvestmentType.AnnuityFund;
return InvestmentType.Unknown;
private InvestmentType(int value, string displayName) : base(value, displayName) { }
public abstract class Enumeration : IComparable
private readonly int _value;
private readonly string _displayName;
protected Enumeration(int value, string displayName)
_displayName = displayName;
public string DisplayName
get { return _displayName; }
public override string ToString()
public static IEnumerable<T> GetAll<T>() where T : Enumeration
var fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
return fields.Select(f => f.GetValue(null)).Cast<T>();
public override bool Equals(object obj)
var otherValue = obj as Enumeration;
var typeMatches = GetType().Equals(obj.GetType());
var valueMatches = _value.Equals(otherValue.Value);
return typeMatches && valueMatches;
public override int GetHashCode()
return _value.GetHashCode();
public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue)
var absoluteDifference = Math.Abs(firstValue.Value - secondValue.Value);
return absoluteDifference;
public static T FromValue<T>(int value) where T : Enumeration
var matchingItem = Parse<T, int>(value, "value", item => item.Value == value);
public static T FromDisplayName<T>(string displayName) where T : Enumeration
var matchingItem = Parse<T, string>(displayName, "display name", item => item.DisplayName == displayName);
private static T Parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration
var matchingItem = GetAll<T>().FirstOrDefault(predicate);
if (matchingItem == null)
var message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(T));
throw new ApplicationException(message);
public int CompareTo(object other)
return Value.CompareTo(((Enumeration)other).Value);