Golan Kiviti
Golan Kiviti

Reputation: 4245

C# - Getting all enums value by attribute

I have this following enum :

public enum KodEnum
{
    [EnumType(EnumType = "Task")]
    TaskTab,
    [EnumType(EnumType = "Task")]
    TaskReason,
    [EnumType(EnumType = "Action")]
    ActionTab,
    [EnumType(EnumType = "Action")]
    ActionReason
}

public class EnumTypeAttribute : Attribute
{
    public string EnumType { get; set; }
}

And I want to get a list of all the enums that have the EnumType of "Task".

How could I do that?

Upvotes: 3

Views: 4677

Answers (3)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

You can do it like:

var result = Enum.GetValues(typeof(KodEnum)).OfType<KodEnum>()  
                 .Where(x=>x.EnumType() == "Task");

here is EnumType extension method implementation:

public static class EnumExtensions
{
   public static string EnumType(this Enum value)  
   {  
       FieldInfo field = value.GetType().GetField(value.ToString());  

       EnumTypeAttribute attribute  
               = Attribute.GetCustomAttribute(field, typeof(EnumTypeAttribute ))  
                   as EnumTypeAttribute;  

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

Upvotes: 0

John
John

Reputation: 3702

Something like this should get you on the way...

var enumValues = (from member in typeof(KodEnum).GetFields()
                  let att = member.GetCustomAttributes(false)
                                  .OfType<EnumTypeAttribute>()
                                  .FirstOrDefault()
                  where att != null && att.EnumType == "Task"
                  select member.GetValue(null))
                 .Cast<KodEnum>()
                 .ToList();

If you want the int value, then just cast it:

var enumValues = (from member in typeof(KodEnum).GetFields()
                  let att = member.GetCustomAttributes(false)
                                  .OfType<EnumTypeAttribute>()
                                  .FirstOrDefault()
                  where att != null && att.EnumType == "Task"
                  select (int)member.GetValue(null))
                 .ToList();

And all-lambda solution:

        var enumValues = typeof(KodEnum)
            .GetFields()
            .Select(x => new 
                { 
                    att = x.GetCustomAttributes(false)
                             .OfType<EnumTypeAttribute>()
                             .FirstOrDefault(), 
                    member = x 
                })
            .Where(x => x.att != null && x.att.EnumType == "Task")
            .Select(x => (int)x.member.GetValue(null))
            .ToList();

Upvotes: 9

Klaus Fischer
Klaus Fischer

Reputation: 135

Keyword is Reflection:

Have you tried something like this?

Enum.GetValues(typeof(KodEnum))
    .OfType<KodEnum>()
    .Where(o => o.GetType()
        .GetCustomAttributes(typeof(EnumTypeAttribute), false)
        .OfType<EnumTypeAttribute>()
        .FirstOrDefault()
        .EnumType == "Task");

Upvotes: -1

Related Questions