C#C
C#2y ago
nut

Getting all types that implement a custom attribute

hey, is using Assembly.GetTypes() and iterating them really the best way to get all types that implement a custom attribute? i was thinking i could just have a static enumerable in the attribute class like so:
class ExampleAttribute : System.Attribute
{
  private static readonly List<ExampleAttribute> implementingTypes = new();
  
  public ExampleAttribute()
  {
    implementingTypes.Add(this);
  }

  public static List<ExampleAttribute> ImplementingTypes => implementingTypes;
}
Was this page helpful?