Beispiel #1
0
        /// <summary>
        /// Converts the enum value into the equivalent header field name.
        /// </summary>
        /// <remarks>
        /// Converts the enum value into the equivalent header field name.
        /// </remarks>
        /// <returns>The header name.</returns>
        /// <param name="value">The enum value.</param>
        public static string ToHeaderName(this HeaderId value)
        {
            var name = value.ToString();

#if NETSTANDARD1_3 || NETSTANDARD1_6
            var field = typeof(HeaderId).GetTypeInfo().GetDeclaredField(name);
            var attrs = field.GetCustomAttributes(typeof(HeaderNameAttribute), false).ToArray();
#else
            var field = typeof(HeaderId).GetField(name);
            var attrs = field.GetCustomAttributes(typeof(HeaderNameAttribute), false);
#endif

            if (attrs != null && attrs.Length == 1)
            {
                return(((HeaderNameAttribute)attrs[0]).HeaderName);
            }

            var builder = new StringBuilder(name);

            for (int i = 1; i < builder.Length; i++)
            {
                if (char.IsUpper(builder[i]))
                {
                    builder.Insert(i++, '-');
                }
            }

            return(builder.ToString());
        }