Here is a universal helper class that will do the opposite - get the key by value from ANY Enum :
public static class EnumHelpers { public static T GetEnumObjectByValue<T>(int valueId) { return (T) Enum.ToObject(typeof (T), valueId); } }
And it works like this: if we have this Enum :
public enum ShipmentStatus { New = 0, Shipped = 1, Canceled = 2 }
So, to get the Enum ShipmentStatus.Shipped object, it will return this object:
var enumObject = EnumHelpers.GetEnumObjectByValue<ShipmentStatus>(1);
So, you can insert any Enum object and get it by value:
var enumObject = EnumHelpers.GetEnumObjectByValue<YOUR_ENUM_TYPE>(VALUE);
mik-t source share