मैं एक और समाधान जोड़ना चाहता हूं: मेरे मामले में, मुझे एक ड्रॉप डाउन बटन सूची आइटम में एनम समूह का उपयोग करने की आवश्यकता है। तो वे अंतरिक्ष हो सकता है, यानी अधिक उपयोगकर्ता के अनुकूल विवरण की जरूरत:
public enum CancelReasonsEnum
{
[Description("In rush")]
InRush,
[Description("Need more coffee")]
NeedMoreCoffee,
[Description("Call me back in 5 minutes!")]
In5Minutes
}
एक सहायक कक्षा में (HelperMethods) मेरे द्वारा बनाए गए निम्न विधि:
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();
}
जब आप इस सहायक फोन आप सूची उपलब्ध करवाई जाएंगी आइटम विवरण का।
List<string> items = HelperMethods.GetListOfDescription<CancelReasonEnum>();
अलावा: किसी भी मामले में, अगर आप इस विधि आप की जरूरत लागू करना चाहते हैं: enum के लिए GetDescription विस्तार। यही है वह जो मेरे द्वारा उपयोग किया जाता है।
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();
*/
}
क्या आपने अभी यह प्रश्न पूछा था ताकि आप इसे स्वयं उत्तर दे सकें? – juharr
आपको अपने प्रश्न पूछने और उत्तर देने की अनुमति है। मेरी चिंता यह होगी कि यह एक डुप्लिकेट सवाल है वैसे भी –
विकी होना चाहिए था! –