以下代碼中的 attribute?.Description中那個?是啥意思?
public static string GetDescription(Enum value, Boolean nameInstead = true) {
try {
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null) {
return null;
}
System.Reflection.FieldInfo field = type.GetField(name);
DescriptionAttribute attribute = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute == null && nameInstead == true) {
return name;
}
return attribute?.Description;
} catch {
return string.Empty;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/250871.html
標籤:C#
