2016-04-12 26 views
3

Enums ile genel olmaya çalışmakla biraz zorlanıyorum. Bu kadar basit olmadığını ve bir çözüm bulamıyorum gibi okudum.Başlarken Tanım Genel bir numaranın Özniteliği

Genel bir işlev oluşturmaya çalışıyorum, bir enum türü için her bir enum değeri için tanım döndürecektir.

:

public static KeyValuePair<string, List<KeyValueDataItem>> ConvertEnumWithDescription<T>() 
    where T : struct, IConvertible 
{ 
    if (!typeof(T).IsEnum) 
    { 
     throw new Exception("Type given T must be an Enum"); 
    } 

    var enumType = typeof(T).ToString().Split('.').Last(); 

    var itemsList = Enum.GetValues(typeof(T)) 
      .Cast<T>() 
      .Select(x => new KeyValueDataItem 
      { 
       Key = Convert.ToInt32(x), 
       Value = GetEnumDescription(Convert.ToInt32(x)) 
      }) 
      .ToList(); 

    var res = new KeyValuePair<string, List<KeyValueDataItem>>(enumType, itemsList); 

    return res; 
} 

public static string GetEnumDescription(Enum value) 
{ 
    FieldInfo fi = value.GetType().GetField(value.ToString()); 

    var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(
      typeof(DescriptionAttribute), false); 

    if (attributes.Length > 0) 
     return attributes[0].Description; 

    return value.ToString(); 
} 

Şu anda yaşadığım sorun şudur: Ben burada

Ben bugüne kadar ne var ... her numaralama türü için bu yöntemi çoğaltmak için o jenerik değil tutmak istiyorum Ben işlevini GetEnumDescription çağırmak çalışıyorum zaman

'System.Enum' için 'int'

dönüştürmek olamaz. Ben T dönüştürmek ise:

Value = GetEnumDescription((T)(object)Convert.ToInt32(x)); 

Bu alıyorum hatadır:

'System.Enum'

cevap

3

, bu deneyin değerler.

here numaralı telefonu izleyebilirsiniz.

public static EnumDescription ConvertEnumWithDescription<T>() where T : struct, IConvertible 
{ 
    if (!typeof(T).IsEnum) 
    { 
     throw new ArgumentException("Type given T must be an Enum"); 
    } 

    var enumType = typeof(T).Name; 
    var valueDescriptions = Enum.GetValues(typeof (T)) 
     .Cast<Enum>() 
     .ToDictionary(Convert.ToInt32, GetEnumDescription); 

    return new EnumDescription 
    { 
     Type = enumType, 
     ValueDescriptions = valueDescriptions 
    }; 

} 

public static string GetEnumDescription(Enum value) 
{ 
    FieldInfo fi = value.GetType().GetField(value.ToString()); 

    DescriptionAttribute[] attributes = 
     (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); 

    if (attributes.Length > 0) 
     return attributes[0].Description; 
    return value.ToString(); 
} 

public class EnumDescription 
{ 
    public string Type { get; set; } 
    public IDictionary<int, string> ValueDescriptions { get; set; } 
} 
1

Daha C# standart API dönüşüne geri dönüş türlerini değiştirmek gibi, kod bazı bölümlerini değiştirerek özgürlüğümü: dayanarak

public static KeyValuePair<string, List<KeyValueDataItem>> ConvertEnumWithDescription<T>() where T : struct, IConvertible 
    { 
     if (!typeof(T).IsEnum) 
     { 
      throw new Exception("Type given T must be an Enum"); 
     } 

     var enumType = typeof(T).ToString().Split('.').Last(); 
     var itemsList = Enum.GetValues(typeof(T)) 
       .Cast<T>() 
       .Select(x => new KeyValueDataItem 
       { 
        Key = Convert.ToInt32(x), 
        Value = GetEnumDescription<T>(x.ToString()) 
       }) 
       .ToList(); 

     var res = new KeyValuePair<string, List<KeyValueDataItem>>(
      enumType, itemsList); 
     return res; 

    } 

    public static string GetEnumDescription<T>(string value) 
    { 
     Type type = typeof(T); 
     var name = Enum.GetNames(type).Where(f => f.Equals(value, StringComparison.CurrentCultureIgnoreCase)).Select(d => d).FirstOrDefault(); 

     if (name == null) 
     { 
      return string.Empty; 
     } 
     var field = type.GetField(name); 
     var customAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute), false); 
     return customAttribute.Length > 0 ? ((DescriptionAttribute)customAttribute[0]).Description : name; 
} 

:

İşte
public static KeyValuePair<string, List<KeyValueDataItem>> ConvertEnumWithDescription<T>() where T : struct, IConvertible 
{ 
     if (!typeof(T).IsEnum) 
     { 
      throw new Exception("Type given T must be an Enum"); 
     } 

     var enumType = typeof(T).ToString().Split('.').Last(); 
     var type = typeof(T); 

     var itemsList = Enum.GetValues(typeof(T)) 
       .Cast<T>() 
       .Select(x => new KeyValueDataItem 
       { 
        Key = Convert.ToInt32(x), 
        Value = GetEnumDescription<T>(Enum.GetName(typeof(T), x)) 
       }) 
       .ToList(); 

     var res = new KeyValuePair<string, List<KeyValueDataItem>>(
      enumType, itemsList); 
     return res; 

}  

public static string GetEnumDescription<T>(string enumValue) 
{ 
    var value = Enum.Parse(typeof(T), enumValue); 
    FieldInfo fi = value.GetType().GetField(value.ToString()); 

    DescriptionAttribute[] attributes = 
     (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); 

    if (attributes.Length > 0) 
     return attributes[0].Description; 
    return value.ToString(); 
}