attribute?.Description是什么意思?
以下代码中的 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;
}
}
...全文
相关推荐
C#如何给枚举类型增加一个描述特性详解
但是,在输出枚举值时,我们通常需要将其转换为对应的汉语意思或其他语句,以便于理解和使用。这时,我们可以使用描述特性来解决这个问题。描述特性是什么?
关于.NET Attribute在数据校验中的应用教程
) { Description = description; }}```然后,我们可以将自定义的Attribute应用到类、方法、字段等程序元素上,例如:```csharppublic class UserEntity
Property和Attribute的区别
我们可以整理出下表,来看同样的概念在OOA/OOD、C++和C#中的不同称呼:| Description | OOA/OOD | C++ | .NET(C#)/Delphi || --- | --- |
C#中Property和Attribute的区别实例详解
**Attribute(属性)**属性(Attribute)则是一种元数据,用于在编译时或运行时为程序元素(如类、方法、字段等)添加附加信息。
C#属性(Attribute)用法实例解析
【C#属性(Attribute)用法实例解析】C#属性是一种元数据,用于向编译器、运行时环境或工具提供额外的信息。


