ben başka bir çözüm eklemek istiyorum: benim durumumda , ben düğme liste öğeleri bir açılır bir Enum grubunu kullanmak gerekir. Böylece boşluk olabilir, yani daha kullanıcı dostu açıklamaları gerekli:
bir yardımcı sınıf olarak
public enum CancelReasonsEnum
{
[Description("In rush")]
InRush,
[Description("Need more coffee")]
NeedMoreCoffee,
[Description("Call me back in 5 minutes!")]
In5Minutes
}
(HelperMethods) Ben aşağıdaki yöntemi oluşturulan: Bu yardımcı çağırdığınızda
public static List<string> GetListOfDescription<T>() where T : struct
{
Type t = typeof(T);
return !t.IsEnum ? null : Enum.GetValues(t).Cast<Enum>().Select(x => x.GetDescription()).ToList();
}
Listeyi alacak ürün açıklamaları.
List<string> items = HelperMethods.GetListOfDescription<CancelReasonEnum>();
ADDITION: Her durumda , ihtiyacınız bu yöntemi uygulamak istiyorsanız: enum için GetDescription uzantısı. Kullandığım bu.
public static string GetDescription(this Enum value)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name != null)
{
FieldInfo field = type.GetField(name);
if (field != null)
{
DescriptionAttribute attr =Attribute.GetCustomAttribute(field,typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attr != null)
{
return attr.Description;
}
}
}
return null;
/* how to use
MyEnum x = MyEnum.NeedMoreCoffee;
string description = x.GetDescription();
*/
}
Bu soruyu siz mi sordunuz? – juharr
Kendi sorunuz için soru sorup cevap verebilirsiniz. Benim endişem, bu zaten yinelenen bir soru olurdu –
Wiki olmalıydı! –