我找不到在 .Net Core 中实现此 DRY 的好方法。(不要重复自己)。我怎样才能做到不重复大部分逻辑?以下是 2 种方法:
public static string GetCategory(this Enum val)
{
CategoryAttribute[] attributes = (CategoryAttribute[])val
.GetType()
.GetField(val.ToString())
.GetCustomAttributes(typeof(CategoryAttribute), false);
return attributes.Length > 0 ? attributes[0].Category : string.Empty;
}
public static string GetDescription(this Enum val)
{
DescriptionAttribute[] attributes = (DescriptionAttribute[])val
.GetType()
.GetField(val.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : string.Empty;
}
森林海
扬帆大鱼
牛魔王的故事
波斯汪
随时随地看视频慕课网APP
相关分类