Dot-Net
dotnet 核心中的列舉描述屬性
我們
Description在 dot net CLI 中有列舉屬性嗎?(Dot Net Core RC2)如果沒有,還有其他選擇嗎?
對於 1.0 和 1.1,
DescriptionAttribute現在在System.ComponentModel.PrimitivesNuGet 包中。
我不得不修改@yaniv 的答案以使用該
DescriptionAttribute類型並獲取該Description欄位。public static class EnumExtensions { /// <summary> /// Get the Description from the DescriptionAttribute. /// </summary> /// <param name="enumValue"></param> /// <returns></returns> public static string GetDescription(this Enum enumValue) { return enumValue.GetType() .GetMember(enumValue.ToString()) .First() .GetCustomAttribute<DescriptionAttribute>()? .Description ?? string.Empty; } }