我一直在寻找一种方式来获得一个枚举的替代值提取备用的数据,并引用这个答案我需要一个优雅的方式来从一个枚举
这使用描述属性分配一个值,然后一个方法用于提取经如此
public static string DescriptionAttr<T>(this T source) { FieldInfo fi = source.GetType().GetField(source.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes( typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) return attributes[0].Description; else return source.ToString(); }
只有我不幸卡在使用.NET 3.5 Compact Framework的黑暗时代,并没有出现有System.ComponentModel.DescriptionAttribute
访问可能有人给我一个提示如何得到这样的工作。 ..
您可以轻松地实现自己的自定义属性,这是你可能需要在这里做什么。 – Crowcoder