本文主要是介绍c# GetDescription,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
public static string? GetDescription(this Enum value, bool nameInstead = true)
{
Type type = value.GetType();
string? name = Enum.GetName(type, value);
if (name == null)return null;
FieldInfo ? field = type.GetField(name);
if (field == null) return null;
DescriptionAttribute? attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute == null && nameInstead) return name;
return attribute?.Description;
}
这篇关于c# GetDescription的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!