如果沒有的話,就一個!你可以在Stackoverflow上找到你需要的其他答案,只需把它們放到一個項目中即可。這裏有幾個讓你開始:
Getting value of enum Description:
public static string GetDescription(this Enum value)
{
FieldInfo field = value.GetType().GetField(value.ToString());
object[] attribs = field.GetCustomAttributes(typeof(DescriptionAttribute), true));
if(attribs.Length > 0)
{
return ((DescriptionAttribute)attribs[0]).Description;
}
return string.Empty;
}
Getting a nullable enum value from string:
public static class EnumUtils
{
public static Nullable<T> Parse<T>(string input) where T : struct
{
//since we cant do a generic type constraint
if (!typeof(T).IsEnum)
{
throw new ArgumentException("Generic Type 'T' must be an Enum");
}
if (!string.IsNullOrEmpty(input))
{
if (Enum.GetNames(typeof(T)).Any(
e => e.Trim().ToUpperInvariant() == input.Trim().ToUpperInvariant()))
{
return (T)Enum.Parse(typeof(T), input, true);
}
}
return null;
}
}
還有全碟庫通用枚舉類型約束: http://code.google.com/p/unconstrained-melody/ – TrueWill 2009-10-22 17:32:57
第一部分是http://stackoverflow.com/questions/17772/anyone-know-a-quick-way-對獲得到自定義屬性上-AN-枚舉值。 – nawfal 2013-06-09 11:59:15