//****************************************************************************************************** // EnumExtensions.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 12/21/2010 - Pinal C. Patel // Generated original version of source code based on code from Tom Fischer. // 12/30/2010 - J. Ritchie Carroll // Corrections / additions. // 12/30/2010 - Pinal C. Patel // Renamed GetEnumFromDescription() to GetEnumValueByDescription() to avoid confusion. // 12/14/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.ComponentModel; using System.Reflection; using System.Text; using GSF.Reflection; namespace GSF { /// /// Defines extension methods related to enumerations. /// public static class EnumExtensions { /// /// Retrieves the description of the value that this represents extracted from the , or the enumeration name /// if no description is available. /// /// to operate on. /// When true, return enum name when no description attribute exists; otherwise, return null. /// /// Description of the if specified, otherwise the representation of this or /// null if is false. public static string GetDescription(this Enum enumeration, bool defaultToName = true) { string name = enumeration.ToString(); string description = enumeration.GetType().GetField(name).GetDescription(); return string.IsNullOrWhiteSpace(description) ? defaultToName ? name : null : description; } /// /// Gets the enumeration constant for this value, if defined in the enumeration, or a default value. /// /// Type of enumeration. /// Value to attempt to return from enumeration. /// Value to return if enumeration value is not found. /// Enumeration value specified, if found, or a default value. /// /// If a default value is not defined, first enumeration value will be used. /// /// is null. /// /// is not an enum. -or- /// The type of is not of type . -or- /// The type of is not an underlying type of . /// /// /// is not type , , /// , , , , /// , or , or . /// public static T GetEnumValueOrDefault(this object value, object defaultValue = null) => (T)value.GetEnumValueOrDefault(typeof(T), defaultValue); /// /// Gets the enumeration constant for value, if defined in the enumeration, or a default value. /// /// Value to attempt to return from enumeration. /// of the enumeration. /// Value to return if enumeration value is not found. /// Enumeration value specified, if found, or a default value. /// /// If a default value is not defined, first enumeration value will be used. /// /// or is null. /// /// is not an enum. -or- /// The type of is not of type . -or- /// The type of is not an underlying type of . /// /// /// is not type , , /// , , , , /// , or , or . /// public static object GetEnumValueOrDefault(this object value, Type type, object defaultValue = null) { if (Enum.IsDefined(type, value)) return value; return defaultValue ?? Enum.GetValues(type).GetValue(0); } /// /// Gets the enumeration of the specified whose description matches this . /// /// Description to be used for lookup of the enumeration. /// of the enumeration. /// true to ignore case during the comparison; otherwise, false. /// An enumeration of the specified if a match is found, otherwise null. /// The is not an enumeration. public static object GetEnumValueByDescription(this string description, Type type, bool ignoreCase = false) { if (!type.IsEnum) throw new ArgumentException("Type must be an enum", nameof(type)); foreach (object value in Enum.GetValues(type)) { if (string.Compare(description, ((Enum)value).GetDescription(), ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0) return value; } return null; } /// /// Gets the enumeration value with the specified name. /// /// Name to search for. /// of the enumeration. /// true to ignore case during the comparison; otherwise, false. /// Specific value of the enumerated constant in terms of its underlying type associated with the specified , or null /// if no matching enumerated value was found. public static object GetEnumValueByName(this string name, Type type, bool ignoreCase = false) { if (!type.IsEnum) throw new ArgumentException("Type must be an enum", nameof(type)); foreach (object value in Enum.GetValues(type)) { if (string.Compare(name, ((Enum)value).ToString(), ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0) return value; } return null; } /// /// Retrieves a formatted name of the value that this represents for visual display. /// /// to operate on. /// Formatted enumeration name of the specified value for visual display. public static string GetFormattedName(this Enum enumeration) { StringBuilder image = new StringBuilder(); char[] chars = enumeration.ToString().ToCharArray(); for (int i = 0; i < chars.Length; i++) { char letter = chars[i]; // Create word spaces at every capital letter if (char.IsUpper(letter) && image.Length > 0) { // Check for all caps sequence (e.g., ID) if (char.IsUpper(chars[i - 1])) { // Look ahead for proper breaking point if (i + 1 < chars.Length) { if (char.IsLower(chars[i + 1])) { image.Append(' '); image.Append(letter); } else { image.Append(letter); } } else { image.Append(letter); } } else { image.Append(' '); image.Append(letter); } } else { image.Append(letter); } } return image.ToString(); } // Internal extension to lookup description from DescriptionAttribute private static string GetDescription(this FieldInfo value) { if (value is null) return string.Empty; return value.TryGetAttribute(out DescriptionAttribute descriptionAttribute) ? descriptionAttribute.Description : string.Empty; } } }