I have to cast enum but I want this to be as generic as possbile. How can I replace the Cast<XX>() part with propertyType?
foreach (var prop in MainType.GetProperties().Where(x => x.PropertyType.IsEnum))
{
     var x = new { name = prop.Name, values = new List<object>() };
     foreach (var v in Enum.GetValues(prop.PropertyType).Cast<XX>())
         x.values.Add(new { p = v.GetAttribute<DescriptionAttribute>().Description, 
                            c = v.GetAttribute<DefaultValueAttribute>().Value });
     o.Add(x);
}
    public static TAttribute GetAttribute<TAttribute>(this Enum value) where TAttribute : Attribute
    {
        var type = value.GetType();
        var name = Enum.GetName(type, value);
        return type.GetField(name)
            .GetCustomAttributes(false)
            .OfType<TAttribute>()
            .SingleOrDefault();
    }
public enum JobApplicationState : short
{
    [Description("Active")]
    [DefaultValue(typeof(string), "bg-primary text-highlight")]
    Active = 1,
    [Description("Passive")]
    [DefaultValue(typeof(string), "bg-grey text-highlight")]
    Passive = 2,
    [Description("Rejected")]
    [DefaultValue(typeof(string), "bg-danger text-highlight")]
    Rejected = 3,
    [Description("Accepted")]
    [DefaultValue(typeof(string), "bg-success text-highlight")]
    Accepted = 4
}
THIS WORKED!
foreach (MemberInfo m in prop.PropertyType.GetFields())
{
    if (m.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault() != null && m.GetCustomAttributes(typeof(DefaultValueAttribute), true).FirstOrDefault() != null)
        {
             x.values.Add(
                 new
                 {
                     p = ((DescriptionAttribute)m.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault()).Description,
                     c = ((DefaultValueAttribute)m.GetCustomAttributes(typeof(DefaultValueAttribute), true).FirstOrDefault()).Value
        });
    } 
}
 
     
     
    