2016-03-21 45 views
1

Orada Custom tarafından Enum alın hangi bir çok örnek Get Enum from Description attributealın Enum Özel Özellik (Genel) tarafından

public static class EnumEx 
{ 
    public static T GetValueFromDescription<T>(string description) 
    { 
     var type = typeof(T); 
     if(!type.IsEnum) throw new InvalidOperationException(); 
     foreach(var field in type.GetFields()) 
     { 
      var attribute = Attribute.GetCustomAttribute(field, 
       typeof(DescriptionAttribute)) as DescriptionAttribute; 
      if(attribute != null) 
      { 
       if(attribute.Description == description) 
        return (T)field.GetValue(null); 
      } 
      else 
      { 
       if(field.Name == description) 
        return (T)field.GetValue(null); 
      } 
     } 
     throw new ArgumentException("Not found.", "description"); 
     // or return default(T); 
    } 
} 

burada gibi niteliklerin Ama burada sorun ÖzellikTürü yani kodlamalısınız olmasıdır vardır typeof(DescriptionAttribute)) as DescriptionAttribute

CustomAttributeType'ı kodlamak zorunda kalmamak için bu örneği Genel uzantıya nasıl dönüştürebilirim.

+1

kullanmak, ama şunu bil ki kullanarak arama yapabilmek için girdi parametresinin anlamı ve kullanımı. Örneğin şimdi bu ölçütlere sahip olabilirsiniz (if (attribute.Description == description) ', ancak jenerik bir parametreyi geçtiğinizde ne olur? –

+2

Bunu nasıl tercüme edersiniz: 'öznitelik' 'descriptionAttribute' değilse' attribute.Description'? – HimBromBeere

cevap

5

Bu dizeleri olarak değerleri karşılaştırmak istediğiniz özellik için çalışacaktır:

var value = GetValueFromAttribute<MyEnum, Description>("desc_text", a => a.Description); 
+0

Tür güvenliğinden emin olmak için func'unuzu 'Invoke' kullanmadan doğrudan çağırabilirsiniz:' if (valueFunc (attribute) == text) '. Ancak gerçekten harika bir cevap. – HimBromBeere

+0

@HimBromBeere Aynı (sadece 'Invoke' kısayolu) –

0

Sen bir arabirim IDescription katkı sağlayabileceğini:

public static TEnum GetValueFromAttribute<TEnum, TAttribute> 
      (string text, Func<TAttribute, string> valueFunc) where TAttribute : Attribute 
{ 
    var type = typeof(TEnum); 
    if(!type.IsEnum) throw new InvalidOperationException(); 
    foreach(var field in type.GetFields()) 
    { 
     var attribute = Attribute.GetCustomAttribute(field, typeof(TAttribute)) as TAttribute; 
     if(attribute != null) 
     { 
      if(valueFunc.Invoke(attribute) == text) 
       return (TEnum)field.GetValue(null); 
     } 
     else 
     { 
      if(field.Name == text) 
       return (TEnum)field.GetValue(null); 
     } 
    } 
    throw new ArgumentException("Not found.", "text"); 
    // or return default(T); 
} 

sonra böyle çağırır öznitelikleriniz uygulandı:

Ya da tam bir tabana eşdeğer sınıfı:

public abstract class BaseDescriptionAttribute : Attribute 
{ 
    public string Description { get; protected set; } 
} 

public static class EnumEx 
{ 
    public static T GetValueFromDescription<T, TAttribute>(string description) where TAttribute : BaseDescriptionAttribute 
    { 
     var type = typeof(T); 

     if (!type.IsEnum) throw new InvalidOperationException(); 

     foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static)) 
     { 
      var attribute = (BaseDescriptionAttribute)Attribute.GetCustomAttribute(field, typeof(TAttribute)); 
0

yeni jenerik tip

public static T GetValueFromDescription<T, K>(string description) 

ekleyin ve yönteme başka jenerik parametre ekleyebilir bir fikir olarak GetCustomerAttribute

var attribute = Attribute.GetCustomAttribute(field, typeof(K)); 
İlgili konular