© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
13 replies
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;
}
class ExampleAttribute : System.Attribute
{
  private static readonly List<ExampleAttribute> implementingTypes = new();
  
  public ExampleAttribute()
  {
    implementingTypes.Add(this);
  }

  public static List<ExampleAttribute> ImplementingTypes => implementingTypes;
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Apply multiple attributes from custom attribute
C#CC# / help
2y ago
Set a class attribute result with a custom attribute
C#CC# / help
3y ago
❔ ✅ Display all properties of different types without an attribute in a WPF list?
C#CC# / help
3y ago
❔ getting all methods with an attribute in an optimized way
C#CC# / help
3y ago