2008-11-17 12 views
6

मैं अपने enumHelper वर्ग इन जिसमें है:जाओ Enum <T> मूल्य विवरण

public enum OutputType 
{ 
    File, 
    [Description("Data Table")] 
    DataTable 
} 

अब तक तो अच्छा:

public static IList<T> GetValues() 
{ 
    IList<T> list = new List<T>(); 
    foreach (object value in Enum.GetValues(typeof(T))) 
    { 
    list.Add((T)value); 
    } 
    return list; 
} 

और

public static string Description(Enum value) 
{ 
    Attribute DescAttribute = LMIGHelper.GetAttribute(value, typeof(DescriptionAttribute)); 
    if (DescAttribute == null) 
    return value.ToString(); 
    else 
    return ((DescriptionAttribute)DescAttribute).Description; 
} 

मेरी enum कुछ की तरह है । सभी पिछले काम ठीक है। अब मैं BindingList वापस जाने के लिए> एक नया सहायक जोड़ना चाहते हैं, तो मैं किसी भी कॉम्बो के लिए किसी भी enum लिंक

BindingList<KeyValuePair<OutputType, string>> list = Enum<OutputType>.GetBindableList(); 
cbo.datasource=list; 
cbo.DisplayMember="Value"; 
cbo.ValueMember="Key"; 

का उपयोग कर सकते हैं कि मैं कहा:

public static BindingList<KeyValuePair<T, string>> GetBindingList() 
{ 
    BindingList<KeyValuePair<T, string>> list = new BindingList<KeyValuePair<T, string>>(); 
    foreach (T value in Enum<T>.GetValues()) 
    { 
     string Desc = Enum<T>.Description(value); 
     list.Add(new KeyValuePair<T, string>(value, Desc)); 
    } 
    return list; 
} 

लेकिन "Enum.Description (मूल्य) "संकलन भी नहीं कर रहा है: तर्क '1': 'टी' से 'सिस्टम' एनम '

मैं इसे कैसे कर सकता हूं? क्या यह भी संभव है?

धन्यवाद।

+0

क्या आपकी विवरण विधि एक विस्तार विधि माना जाता है? यदि ऐसा है, तो आप इस कीवर्ड को याद कर चुके हैं। –

+0

इस प्रश्न पर मेरा उत्तर देखें https://stackoverflow.com/questions/6145888/how-to-bind-an-enum-to-a-combobox-control-in-wpf/12430331#12430331 – Nick

उत्तर

-2

Enum में कोई विवरण() विधि नहीं है। सबसे अच्छा आप कर सकते हैं कि आपका enum एक इंटरफ़ेस को कार्यान्वित करता है जिसमें विवरण() विधि है। आप ऐसा है, तो आप

public static BindingList<KeyValuePair<T extends _interface_, String>> getBindingList() 

और फिर हो सकता है के अंदर आप

T foo = ...? 
foo.Description(...); 
3

का उल्लेख कर सकते आप बदलना चाहिए:

public static string Description(Enum value) 
{ 
    ... 
} 

public static string Description(T value) 
{ 
    ... 
} 
को

इसलिए यह गणना के मूल्य को स्वीकार करता है। अब यहां यह मुश्किल है: यह आपके पास एक मूल्य है, लेकिन गुण फ़ील्ड को सजाने वाले क्षेत्र में मानते हैं।

आप वास्तव में गणना के क्षेत्रों पर विचार करने और मूल्य आप दिया गया है के खिलाफ प्रत्येक की कीमत की जांच करने की जरूरत है (परिणाम प्रदर्शन के लिए कैश की गई जानी चाहिए):

foreach(var field in typeof(T).GetFields()) 
{ 
    T fieldValue; 

    try 
    { 
     fieldValue = (T) field.GetRawConstantValue(); 
    } 
    catch(InvalidOperationException) 
    { 
     // For some reason, one of the fields returned is {Int32 value__}, 
     // which throws an InvalidOperationException if you try and retrieve 
     // its constant value. 
     // 
     // I am unsure how to check for this state before 
     // attempting GetRawConstantValue(). 

     continue; 
    } 

    if(fieldValue == value) 
    { 
     var attribute = LMIGHelper.GetAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; 

     return attribute == null ? value.ToString() : attribute.Description; 
    } 
} 

संपादित अनुवर्ती को संबोधित प्रश्न

FillComboFromEnum विधि enum के लिए प्रकार पैरामीटर गुम है। इसे आज़माएं:

public static void FillComboFromEnum<T>(ComboBox Cbo, BindingList<KeyValuePair<T, string>> List) where T : struct 

नोटिस मैंने इस प्रकार को एक संरचना होने के लिए बाध्य किया। यह एक पूर्ण गणना बाधा नहीं है, लेकिन यह कुछ भी नहीं है।

6

इस article पर एक नज़र डालें। आप System.ComponentModel का उपयोग कर ऐसा कर सकते हैं।DescriptionAttribute या अपने खुद के गुण बनाने:

/// <summary> 
/// Provides a description for an enumerated type. 
/// </summary> 
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field, 
AllowMultiple = false)] 
public sealed class EnumDescriptionAttribute : Attribute 
{ 
    private string description; 

    /// <summary> 
    /// Gets the description stored in this attribute. 
    /// </summary> 
    /// <value>The description stored in the attribute.</value> 
    public string Description 
    { 
     get 
     { 
     return this.description; 
     } 
    } 

    /// <summary> 
    /// Initializes a new instance of the 
    /// <see cref="EnumDescriptionAttribute"/> class. 
    /// </summary> 
    /// <param name="description">The description to store in this attribute. 
    /// </param> 
    public EnumDescriptionAttribute(string description) 
     : base() 
    { 
     this.description = description; 
    } 
} 

तो आप इस नई विशेषता के साथ enum मूल्यों को सजाने के लिए की जरूरत है:

public enum SimpleEnum 
{ 
    [EnumDescription("Today")] 
    Today, 

    [EnumDescription("Last 7 days")] 
    Last7, 

    [EnumDescription("Last 14 days")] 
    Last14, 

    [EnumDescription("Last 30 days")] 
    Last30, 

    [EnumDescription("All")] 
    All 
} 

"जादू" के सभी निम्नलिखित विस्तार के तरीकों में जगह लेता है:

:
/// <summary> 
/// Provides a static utility object of methods and properties to interact 
/// with enumerated types. 
/// </summary> 
public static class EnumHelper 
{ 
    /// <summary> 
    /// Gets the <see cref="DescriptionAttribute" /> of an <see cref="Enum" /> 
    /// type value. 
    /// </summary> 
    /// <param name="value">The <see cref="Enum" /> type value.</param> 
    /// <returns>A string containing the text of the 
    /// <see cref="DescriptionAttribute"/>.</returns> 
    public static string GetDescription(this Enum value) 
    { 
     if (value == null) 
     { 
     throw new ArgumentNullException("value"); 
     } 

     string description = value.ToString(); 
     FieldInfo fieldInfo = value.GetType().GetField(description); 
     EnumDescriptionAttribute[] attributes = 
     (EnumDescriptionAttribute[]) 
     fieldInfo.GetCustomAttributes(typeof(EnumDescriptionAttribute), false); 

     if (attributes != null && attributes.Length > 0) 
     { 
     description = attributes[0].Description; 
     } 
     return description; 
    } 

    /// <summary> 
    /// Converts the <see cref="Enum" /> type to an <see cref="IList" /> 
    /// compatible object. 
    /// </summary> 
    /// <param name="type">The <see cref="Enum"/> type.</param> 
    /// <returns>An <see cref="IList"/> containing the enumerated 
    /// type value and description.</returns> 
    public static IList ToList(this Type type) 
    { 
     if (type == null) 
     { 
     throw new ArgumentNullException("type"); 
     } 

     ArrayList list = new ArrayList(); 
     Array enumValues = Enum.GetValues(type); 

     foreach (Enum value in enumValues) 
     { 
     list.Add(new KeyValuePair<Enum, string>(value, GetDescription(value))); 
     } 

     return list; 
    } 
} 

अंत में, आप तो बस बता गया बाध्य कर सकते हैं

+0

ओउर आप इसे लिख सकते हैं आपके जवाब में – Henrik

+0

कम से कम एक सारांश। – Henrik

+0

क्या यहां ऐरेलिस्ट का उपयोग करने का कोई विशिष्ट कारण है? सूचीबद्ध नहीं होगा? – nawfal