Can you use “where” to require an attribute in c#?

Nope, I’m afraid not. The only things you can do with constraints are:

  • where T : class – T must be a reference type
  • where T : struct – T must be a non-nullable value type
  • where T : SomeClass – T must be SomeClass or derive from it
  • where T : ISomeInterface – T must be ISomeInterface or implement it
  • where T : new() – T must have a public parameterless constructor

Various combinations are feasible, but not all. Nothing about attributes.

Leave a Comment