コード例 #1
0
ファイル: EnumHelper.cs プロジェクト: cirpitch/AspNetCore
        /// <summary>
        ///     Get the display text of an enum item from the specified text source.
        /// </summary>
        /// <param name="memberInfo">The <see cref="MemberInfo" /> object represented as an enum item.</param>
        /// <param name="textSource">The text source for the enum item.</param>
        /// <returns>
        ///     The text retrieved from the <paramref name="memberInfo" /> definition. If there is no text in the location
        ///     <paramref name="textSource" /> specified, this method will return <see cref="MemberInfo.Name" />.
        /// </returns>
        /// <exception cref="ArgumentException">The value of <paramref name="textSource" /> is not a valid enum item.</exception>
        public static string GetTextForMember(this MemberInfo memberInfo, EnumOptionTextSource textSource)
        {
            // Get DisplayAttribute instance
            var attr = memberInfo.GetCustomAttribute <DisplayAttribute>();

            // No attribute is defined, return name immediately
            if (attr == null)
            {
                return(memberInfo.Name);
            }

            // Variable to store the result
            string result;

            // Get data according to the source
            switch (textSource)
            {
            case EnumOptionTextSource.EnumNameOnly:
                result = memberInfo.Name;
                break;

            case EnumOptionTextSource.Name:
                result = attr.GetName();
                break;

            case EnumOptionTextSource.ShortName:
                result = attr.GetShortName();
                break;

            case EnumOptionTextSource.Description:
                result = attr.GetDescription();
                break;

            default:
                throw new ArgumentException("The argument value is not a valid enum item.", nameof(textSource));
            }

            // No result it found from the source, fallback to name
            if (string.IsNullOrEmpty(result))
            {
                result = memberInfo.Name;
            }

            // Return
            return(result);
        }
コード例 #2
0
ファイル: EnumHelper.cs プロジェクト: sgjsakura/AspNetCore
		/// <summary>
		///     Get the display text of an enum item from the specified text source.
		/// </summary>
		/// <param name="memberInfo">The <see cref="MemberInfo" /> object represented as an enum item.</param>
		/// <param name="textSource">The text source for the enum item.</param>
		/// <returns>
		///     The text retrieved from the <paramref name="memberInfo" /> definition. If there is no text in the location
		///     <paramref name="textSource" /> specified, this method will return <see cref="MemberInfo.Name" />.
		/// </returns>
		/// <exception cref="ArgumentException">The value of <paramref name="textSource" /> is not a valid enum item.</exception>
		public static string GetTextForMember(this MemberInfo memberInfo, EnumOptionTextSource textSource)
		{
			// Get DisplayAttribute instance
			var attr = memberInfo.GetCustomAttribute<DisplayAttribute>();

			// No attribute is defined, return name immediately
			if (attr == null)
			{
				return memberInfo.Name;
			}

			// Variable to store the result
			string result;

			// Get data according to the source
			switch (textSource)
			{
				case EnumOptionTextSource.EnumNameOnly:
					result = memberInfo.Name;
					break;
				case EnumOptionTextSource.Name:
					result = attr.GetName();
					break;
				case EnumOptionTextSource.ShortName:
					result = attr.GetShortName();
					break;
				case EnumOptionTextSource.Description:
					result = attr.GetDescription();
					break;
				default:
					throw new ArgumentException("The argument value is not a valid enum item.", nameof(textSource));
			}

			// No result it found from the source, fallback to name
			if (string.IsNullOrEmpty(result))
			{
				result = memberInfo.Name;
			}

			// Return
			return result;
		}