Sakura.AspNetCore.Extensions/Sakura.AspNetCore.Mvc.TagHelpers/ReflectionHelper.cs
/// <summary>
///     Get the display text of an <see cref="MemberInfo" /> from the specified text source.
/// </summary>
/// <param name="memberInfo">The <see cref="MemberInfo" /> object.</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>
Get the display text of an  from the specified text source.
Get the display text of an from the specified text source .
public static string GetTextForMember(this MemberInfo memberInfo, TextSource textSource)
		{
			var attr = memberInfo.GetCustomAttribute<DisplayAttribute>();
			if (attr == null)
				return memberInfo.Name;
			string result;
			switch (textSource)
			{
				case TextSource.MemberNameOnly:
					result = memberInfo.Name;
					break;
				case TextSource.Name:
					result = attr.GetName();
					break;
				case TextSource.ShortName:
					result = attr.GetShortName();
					break;
				case TextSource.Description:
					result = attr.GetDescription();
					break;
				default:
					throw new ArgumentException("The argument value is not a valid enum item.", nameof(textSource));
			}
			if (string.IsNullOrEmpty(result))
				result = memberInfo.Name;
			return result;
		}
public static string GetTextForMember ( this MemberInfo memberInfo , TextSource textSource ) { var attr = memberInfo . GetCustomAttribute < DisplayAttribute > ( ) ; if ( attr == null ) return memberInfo . Name ; string result ; switch ( textSource ) { case TextSource . MemberNameOnly : result = memberInfo . Name ; break ; case TextSource . Name : result = attr . GetName ( ) ; break ; case TextSource . ShortName : result = attr . GetShortName ( ) ; break ; case TextSource . Description : result = attr . GetDescription ( ) ; break ; default : throw new ArgumentException ( " " , nameof ( textSource ) ) ; } if ( string . IsNullOrEmpty ( result ) ) result = memberInfo . Name ; return result ; }
Get the display text of an  from the specified text source.
Get the display text of an from the specified text source .
// Get DisplayAttribute instance // No attribute is defined, return name immediately // Variable to store the result // Get data according to the source // No result it found from the source, fallback to name // Return
The text retrieved from the  definition. If there is no text in the location
specified, this method will return .
The text retrieved from the definition . If there is no text in the location specified this method will return .
The value of  is not a valid enum item.
The value of is not a valid enum item .
MemberInfo
this
textSource
TextSource
The text source for the enum item.
The text source for the enum item .
memberInfo
None