/// <summary>Obtains the EnumMemberAttribute.Value property or enumeration ToString().</summary> /// <param name="type">Type of enumeration being passed in.</param> /// <param name="field">Value of the enumeration being passed in.</param> /// <returns>EnumMemberAttribute or resourceType.ToString().</returns> public static string ToAttributeValue(Type type, Enum field) { Guard.AgainstNull(type); Guard.AgainstNull(field); if (!type.IsEnum) { throw new NotImplementedException(); } EnumMemberAttribute attribute = EnumExtensions.GetEnumMemberAttribute(type, field.ToString()); return(attribute == null || string.IsNullOrEmpty(attribute.Value) ? field.ToString() : attribute.Value); }
/// <summary>Gets the enum from EnumMemberAttribute.</summary> /// <param name="enumType">Type of the enum.</param> /// <param name="fieldValue">The field value.</param> /// <returns></returns> private static Enum GetEnumFromAttribute(Type enumType, string fieldValue) { if (!enumType.IsEnum) { throw new NotImplementedException(); } foreach (var value in Enum.GetValues(enumType)) { EnumMemberAttribute attribute = EnumExtensions.GetEnumMemberAttribute(enumType, value.ToString()); if (attribute != null && string.Equals(attribute.Value, fieldValue)) { return((Enum)value); } } return(null); }