I have a custom attribute with a bool value. I want to return the [Description] only for enum values where the custom value is true.
Sample:
[Description("Concept Set Model")]
[ModelIsDesignTimeDefinable(true)]
ConceptSet = 1 << 24
I have these two extension methods, but they're obviously incorrect because they need to work together:
public static IEnumerable<string> ExtractDescriptionAttrib(this IEnumerable<string> set, Type type) => set
.Select(n => type.GetMember(n).First())
.SelectMany(member => member
.GetCustomAttributes(typeof(DescriptionAttribute), true)
.Cast<DescriptionAttribute>())
.Select(x => x.Description);
public static IEnumerable<bool> ExtractDefinableAttrib(this IEnumerable<string> set, Type type) => set
.Select(n => type.GetMember(n).First())
.SelectMany(member => member
.GetCustomAttributes(typeof(ModelIsDesignTimeDefinableAttribute), true)
.Cast<ModelIsDesignTimeDefinableAttribute>())
.Select(x => x.Definable);
My original calling code:
public static IEnumerable<string> GetModelDescriptions()
{
var type = typeof(ModelType);
var set = Enum.GetValues(type)
.Cast<ModelType>()
.Where(x => x > 0)
.Select(x => x.ToString());
// return with " Model" clipped off the end
return set
.ExtractDescriptionAttrib(type)
.Select(x => x[0..^6]);
}
I'm sure the solution is straightforward, but I'm missing it.