I want to return a list of enum members that have specific attribute. So for the code example I would like a list with enums SecondValue and ThirdValue, but not FirstValue or FourthValue because it doesn't have the attribute Container.
How could I do this?
PS: This was marked as duplicate before with link to this: Getting attributes of Enum's value
This is NOT the same question. I made more example code to make my question more clear. See the function definition and what it should return.
[AttributeUsage(AttributeTargets.Field)]
public sealed class ContainerAttribute : Attribute
{
public string Name { get; }
public ContainerAttribute(string name)
{
Name = name;
}
}
public enum MyEnum
{
[SomeOtherAttribute("attr")]
FirstValue,
[Container("name1")]
SecondValue,
[Container("name1")]
ThirdValue,
FourthValue
}
public List<MyEnum> GetEnumsWithAttribute(Attribute value) { }
public void Main(String[] args) {
// someEnums should return list with content: [MyEnum.SecondValue, MyEnum.ThirdValue];
var someEnums = GetEnumsWithAttribute(ContainerAttribute);
}