Search code examples
.nettype-constraints

Attribute constraint on a generic type in .net?


in .net, if I have a generic class SomeClass<T>, is it possible to use the where keyword to require that T is a class with a certain attribute? something like:

[SomeAttribute]
class MyClass
{
    ...
}

class AnotherClass<T> where T : Attribute(SomeAttribute)
{
    ...
}

Solution

  • No, that's not possible.

    The closest you can do is to require that the class implement a particular interface.