Skip to content

Add Object to Enum converter

Latest

Choose a tag to compare

@alyiox alyiox released this 24 Oct 03:56
· 35 commits to main since this release
        /// <summary>
        /// Converts the value of the specified object to an enumeration type..
        /// </summary>
        /// <typeparam name="T">An enumeration type.</typeparam>
        /// <param name="value">An object that supplies the value to convert, or null.</param>
        /// <returns>An object of type enumType whose value is represented by value.</returns>
        public static T? ToEnum<T>(this object? value) where T : struct, Enum
        {
            if (value is T v)
            {
                return v;
            }

            var text = value?.ToString();
            return Enum.TryParse(text, true, out T e) ? e : default(T);
        }